不会吧,学过爬虫连这个网站都爬不了?那Python岂不是白学了

发布时间:2022-07-01 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了不会吧,学过爬虫连这个网站都爬不了?那Python岂不是白学了脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文内容

  1. 系统分析目标网页
  2. htML标签数据解析方法
  3. 海量图片数据一键保存

环境介绍

模块使用

  • requests >>> pip install requests
  • parsel >>> pip install parsel
  • time 时间模块 记录运行时间

通用爬虫

导入模块

import requests  # 数据请求模块 第三方模块 pip install requests
import parsel  # 数据解析模块 第三方模块 pip install parsel
import re  # 正则表达式模块

请求数据

url = f'https://fabiaoqing.COM/biaoqing/lists/page/{page}html'
headers = {
    'User-Agent': ';mozilla/5.0 (Windows NT 10.0; Win64; x64) Applewebkit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
# <Response [200]> response 对象 200状态码 表示请求成功

解析数据

解析速度 bs4 解析速度会慢一些,如果你想要对于字符串数据内容,直接取值,只能正则表达式

selector = parsel.Selector(response.text) # 把获取下来html字符串数据内容 转成 selector 对象
tITle_list = selector.css('.ui.image.lazy::attr(title)').getall()
img_list = selector.css('.ui.image.lazy::attr(data-original)').getall()
# 把获取下来的这两个列表 提取里面元素 一一提取出来
# 提取列表元素 for循环 遍历
for title, img_url in zip(title_list, img_list):
    title = re.sub(r'[/:*?"<>|n]', '_', title)
    # 名字太长 报错
    img_name = img_url.split('.')[-1]   # 通过split() 字符串分割的方法 根据列表索引位置取值
    img_content = requests.get(url=img_url).content # 获取图片的二进制数据内容

保存数据

with oPEn('img\' + title + '.' + img_name, mode='wb') as f:
    f.write(img_content)
PRint(title)

不会吧,学过爬虫连这个网站都爬不了?那Python岂不是白学了

共耗时F1a;61秒

不会吧,学过爬虫连这个网站都爬不了?那Python岂不是白学了

多线程爬虫

发送求情

def get_response(html_url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36'
    }
    response = requests.get(url=html_url, headers=headers)
    return response

获取图片url地址,以及图片名字

def get_img_info(html_url):
    response = get_response(html_url)
    selector = parsel.Selector(response.text)  # 把获取下来html字符串数据内容 转成 selector 对象
    title_list = selector.css('.ui.image.lazy::attr(title)').getall()
    img_list = selector.css('.ui.image.lazy::attr(data-original)').getall()
    zip_data = zip(title_list, img_list)
    return zip_data

保存数据

def save(title, img_url):
    title = re.sub(r'[/:*?"<>|n]', '_', title)
    # 名字太长 报错
    img_name = img_url.split('.')[-1]  # 通过split() 字符串分割的方法 根据列表索引位置取值
    img_content = requests.get(url=img_url).content  # 获取图片的二进制数据内容
    with open('img\' + title + '.' + img_name, mode='wb') as f:
        f.write(img_content)
    print(title)

主函数

def main(html_url):
    zip_data = get_img_info(html_url)
    for title, img_url in zip_data:
        save(title, img_url)

入口

if __name__ == '__main__':
    start_time = time.time()
    exe = concurrent.Futures.ThreadPoolExecutor(max_workers=10)
    for page in range(1, 11):
        url = f'https://fabiaoqing.com/biaoqing/lists/page/{page}html'
        exe.submit(main, url)
    exe.shutdown()
    end_time = time.time()
    use_time = int(end_time - start_time)
    print('程序耗时: ', use_time)

不会吧,学过爬虫连这个网站都爬不了?那Python岂不是白学了

共耗时:19秒

对于文章有疑问,或者想要Python相关资料、码的同学也可以点这里

脚本宝典总结

以上是脚本宝典为你收集整理的不会吧,学过爬虫连这个网站都爬不了?那Python岂不是白学了全部内容,希望文章能够帮你解决不会吧,学过爬虫连这个网站都爬不了?那Python岂不是白学了所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。