脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python基础进阶之海量表情包多线程爬虫功能的实现

Python基础进阶之海量表情包多线程爬虫功能的实现

2021-08-15 11:23孤城暮雨 Python

这篇文章主要介绍了Python基础进阶之海量表情包多线程爬虫,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

一、前言

在我们日常聊天的过程中会使用大量的表情包,那么如何去获取表情包资源呢?今天老师带领大家使用python中的爬虫去一键下载海量表情包资源

二、知识点

requests网络库
bs4选择器
文件操作
多线程

三、所用到得库

?
1
2
3
import os
import requests
from bs4 import BeautifulSoup

四、 功能

?
1
2
3
4
# 多线程程序需要用到的一些包
# 队列
from queue import Queue
from threading import Thread

五、环境配置

解释器 python3.6
编辑器 pycharm专业版 激活码

六、多线程类代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 多线程类
class Download_Images(Thread):
  # 重写构造函数
  def __init__(self, queue, path):
    Thread.__init__(self)
    # 类属性
    self.queue = queue
    self.path = path
    if not os.path.exists(path):
      os.mkdir(path)
  def run(self) -> None:
    while True:
      # 图片资源的url链接地址
      url = self.queue.get()
      try:
        download_images(url, self.path)
      except:
        print('下载失败')
      finally:
        # 当爬虫程序执行完成/出错中断之后发送消息给线程 代表线程必须停止执行
        self.queue.task_done()

七、爬虫代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 爬虫代码
def download_images(url, path):
  headers = {
    'User-Agent':
      'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
  }
  response = requests.get(url, headers=headers)
  soup = BeautifulSoup(response.text, 'lxml')
  img_list = soup.find_all('img', class_='ui image lazy')
  for img in img_list:
    image_title = img['title']
    image_url = img['data-original']
 
    try:
      with open(path + image_title + os.path.splitext(image_url)[-1], 'wb') as f:
        image = requests.get(image_url, headers=headers).content
        print('正在保存图片:', image_title)
        f.write(image)
        print('保存成功:', image_title)
    except:
      pass
 
if __name__ == '__main__':
  _url = 'https://fabiaoqing.com/biaoqing/lists/page/{page}.html'
  urls = [_url.format(page=page) for page in range(1, 201)]
  queue = Queue()
  path = './threading_images/'
  for x in range(10):
    worker = Download_Images(queue, path)
    worker.daemon = True
    worker.start()
  for url in urls:
    queue.put(url)
  queue.join()
  print('下载完成...')

八、爬取效果图片

Python基础进阶之海量表情包多线程爬虫功能的实现

到此这篇关于Python基础进阶之海量表情包多线程爬虫的文章就介绍到这了,更多相关Python多线程爬虫内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/aa1273935919/archive/2020/12/17/14149402.html

延伸 · 阅读

精彩推荐