网络编程
位置:首页>> 网络编程>> Python编程>> Python实现多线程爬表情包详解

Python实现多线程爬表情包详解

作者:魔王不会哭  发布时间:2022-05-26 01:30:55 

标签:Python,多线程,爬表情包

课程亮点

系统分析目标网页

html标签数据解析方法

海量图片数据一键保存

环境介绍

python 3.8

pycharm

模块使用

requests >>> pip install requests

parsel >>> pip install parsel

time 时间模块 记录运行时间

流程

一. 分析我们想要的数据内容 是可以从哪里获取

表情包 >>> 图片url地址 以及 图片名字

对于开发者工具的使用 >>>

二. 代码实现步骤

1.发送请求

确定一下发送请求 url地址

请求方式是什么 get请求方式 post请求方式

请求头参数 : 防盗链 cookie …

2.获取数据

获取服务器返回的数据内容

response.text 获取文本数据

response.json() 获取json字典数据

response.content 获取二进制数据 保存图片/音频/视频/特定格式文件内容 都是获取二进制数据内容

3.解析数据

提取我们想要的数据内容

I. 可以直接解析处理

II. json字典数据 键值对取值

III. re正则表达式

IV. css选择器

V. xpath

4.保存数据

文本

csv

数据库

本地文件夹

导入模块


import requests  # 数据请求模块 第三方模块 pip install requests
import parsel  # 数据解析模块 第三方模块 pip install parsel
import re  # 正则表达式模块
import time  # 时间模块
import concurrent.futures

单线程爬取10页数据

1. 发送请求


start_time = time.time()

for page in range(1, 11):
   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状态码 表示请求成功

2. 获取数据, 获取文本数据 / 网页源代码


# 在开发者工具上面 元素面板 看到有相应标签数据, 但是我发送请求之后 没有这样的数据返回
# 我们要提取数据, 要根据服务器返回数据内容
# xpath 解析方法 parsel 解析模块  parsel这个模块里面就可以调用xpath解析方法
# print(response.text)

3. 解析数据


# 解析速度 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):

4. 保存数据


# split() 字符串分割的方法 根据列表索引位置取值
# img_name_1 = img_url[-3:] # 通过字符串数据 进行切片
# 从左往右 索引位置 是从 0 开始 从右往左 是 -1开始
        # print(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)

多线程爬取10页数据


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

def get_img_info(html_url):
   """获取图片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)

多进程爬取10页数据


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):
       # 1. 发送请求
       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)

单线程爬取10页数据 61秒时间

多线程爬取10页数据 19秒时间 >>> 13

多进程爬取10页数据 21秒时间 >>> 18

来源:https://blog.csdn.net/python56123/article/details/121471909?spm=1001.2014.3001.5501

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com