Python如何使用队列方式实现多线程爬虫


Posted in Python onMay 12, 2020

说明:糗事百科段子的爬取,采用了队列和多线程的方式,其中关键点是Queue.task_done()、Queue.join(),保证了线程的有序进行。

代码如下

import requests
from lxml import etree
import json
from queue import Queue
import threading

class Qsbk(object):
  def __init__(self):
    self.headers = {
      "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36",
      "Referer": "https://www.qiushibaike.com/"
    }
    # 实例化三个队列,用来存放内容
    self.url_queue = Queue()
    self.html_queue = Queue()
    self.content_queue = Queue()

  def get_total_url(self):
    """
    获取了所有的页面url,并且返回url_list
    return:url_list
    现在放入url_queue队列中保存
    """
    url_temp = "https://www.qiushibaike.com/text/page/{}/"
    url_list = list()
    for i in range(1,13):
      # url_list.append(url_temp.format(i))
      # 将生成的url放入url_queue队列
      self.url_queue.put(url_temp.format(i))

  def parse_url(self):
    """
    发送请求,获取响应,同时etree处理html
    """
    while self.url_queue.not_empty:
      # 判断非空,为空时结束循环

      # 从队列中取出一个url
      url = self.url_queue.get()
      print("parsing url:",url)
      # 发送请求
      response = requests.get(url,headers=self.headers,timeout=10)
      # 获取html字符串
      html = response.content.decode()
      # 获取element类型的html
      html = etree.HTML(html)
      # 将生成的element对象放入html_queue队列
      self.html_queue.put(html)
      # Queue.task_done() 在完成一项工作之后,Queue.task_done()函数向任务已经完成的队列发送一个信号
      self.url_queue.task_done()

  def get_content(self):
    """
    解析网页内容,获取想要的信息
    """
    while self.html_queue.not_empty:
      items = list()
      html = self.html_queue.get()
      total_div = html.xpath("//div[@class='col1 old-style-col1']/div")
      for i in total_div:

        author_img = i.xpath(".//a[@rel='nofollow']/img/@src")
        author_img = "https"+author_img[0] if len(author_img)>0 else None

        author_name = i.xpath(".//a[@rel='nofollow']/img/@alt")
        author_name = author_name[0] if len(author_name)>0 else None

        author_href = i.xpath("./a/@href")
        author_href = "https://www.qiushibaike.com/"+author_href[0] if len(author_href)>0 else None

        author_gender = i.xpath("./div[1]/div/@class")
        author_gender = author_gender[0].split(" ")[-1].replace("Icon","").strip() if len(author_gender)>0 else None

        author_age = i.xpath("./div[1]/div/text()")
        author_age = author_age[0] if len(author_age)>0 else None

        content = i.xpath("./a/div/span/text()")
        content = content[0].strip() if len(content)>0 else None

        content_vote = i.xpath("./div[@class='stats']/span[@class='stats-vote']/i/text()")
        content_vote = content_vote[0] if len(content_vote)>0 else None

        content_comment_numbers = i.xpath("./div[@class='stats']/span[@class='stats-comments']/a/i/text()")
        content_comment_numbers = content_comment_numbers[0] if len(content_comment_numbers)>0 else None

        item = {
          "author_name":author_name,
          "author_age" :author_age,
          "author_gender":author_gender,
          "author_img":author_img,
          "author_href":author_href,
          "content":content,
          "content_vote":content_vote,
          "content_comment_numbers":content_comment_numbers,
        }
        items.append(item)
      self.content_queue.put(items)
      # task_done的时候,队列计数减一
      self.html_queue.task_done()

  def save_items(self):
    """
    保存items
    """
    while self.content_queue.not_empty:
      items = self.content_queue.get()
      with open("quishibaike.txt",'a',encoding='utf-8') as f:
        for i in items:
          json.dump(i,f,ensure_ascii=False,indent=2)
      self.content_queue.task_done()

  def run(self):
    # 获取url list
    thread_list = list()
    thread_url = threading.Thread(target=self.get_total_url)
    thread_list.append(thread_url)

    # 发送网络请求
    for i in range(10):
      thread_parse = threading.Thread(target=self.parse_url)
      thread_list.append(thread_parse)

    # 提取数据
    thread_get_content = threading.Thread(target=self.get_content)
    thread_list.append(thread_get_content)

    # 保存
    thread_save = threading.Thread(target=self.save_items)
    thread_list.append(thread_save)


    for t in thread_list:
      # 为每个进程设置为后台进程,效果是主进程退出子进程也会退出
      t.setDaemon(True)
      t.start()
    
    # 让主线程等待,所有的队列为空的时候才能退出
    self.url_queue.join()
    self.html_queue.join()
    self.content_queue.join()


if __name__=="__main__":
  obj = Qsbk()
  obj.run()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
在Python的Django框架中为代码添加注释的方法
Jul 16 Python
在Django的模型和公用函数中使用惰性翻译对象
Jul 27 Python
python 使用 requests 模块发送http请求 的方法
Dec 09 Python
Python3.5内置模块之shelve模块、xml模块、configparser模块、hashlib、hmac模块用法分析
Apr 27 Python
python程序快速缩进多行代码方法总结
Jun 23 Python
如何爬取通过ajax加载数据的网站
Aug 15 Python
Tensorflow中的降维函数tf.reduce_*使用总结
Apr 20 Python
如何在keras中添加自己的优化器(如adam等)
Jun 19 Python
keras的ImageDataGenerator和flow()的用法说明
Jul 03 Python
Python3 搭建Qt5 环境的方法示例
Jul 16 Python
python高级特性简介
Aug 13 Python
python中entry用法讲解
Dec 04 Python
python的Jenkins接口调用方式
May 12 #Python
jenkins+python自动化测试持续集成教程
May 12 #Python
python百行代码自制电脑端网速悬浮窗的实现
May 12 #Python
基于Python的Jenkins的二次开发操作
May 12 #Python
Python-jenkins模块获取jobs的执行状态操作
May 12 #Python
Python-jenkins 获取job构建信息方式
May 12 #Python
python进行参数传递的方法
May 12 #Python
You might like
相对路径转化成绝对路径
2007/04/10 PHP
phpmail类发送邮件函数代码
2012/02/20 PHP
php权重计算方法代码分享
2014/01/09 PHP
php操作xml入门之xml标签的属性分析
2015/01/23 PHP
json 定义
2008/06/10 Javascript
JavaScript 动态生成方法的例子
2009/07/22 Javascript
Javascript JSQL,SQL无处不在,
2010/05/05 Javascript
javascript之典型高阶函数应用介绍二
2013/01/10 Javascript
jquery重复提交请求的原因浅析
2014/05/23 Javascript
js实现页面跳转的五种方法推荐
2016/03/10 Javascript
Javascript将字符串日期格式化为yyyy-mm-dd的方法
2016/10/27 Javascript
详解微信小程序 页面跳转 传递参数
2016/12/08 Javascript
js中开关变量使用实例
2017/02/24 Javascript
vue中实现滚动加载更多的示例
2017/11/08 Javascript
vue中使用element-ui进行表单验证的实例代码
2018/06/22 Javascript
解决vue移动端适配问题
2018/12/12 Javascript
利用Dectorator分模块存储Vuex状态的实现
2019/02/05 Javascript
JS实现处理时间,年月日,星期的公共方法示例
2019/05/31 Javascript
[04:49]2014DOTA2国际邀请赛 Newbee顺利挺进总决赛 ImbaTV独家专访
2014/07/19 DOTA
python 快速排序代码
2009/11/23 Python
python入门基础之用户输入与模块初认识
2016/11/14 Python
python如何派生内置不可变类型并修改实例化行为
2018/03/21 Python
Python单向链表和双向链表原理与用法实例详解
2018/08/31 Python
使用Python编写Prometheus监控的方法
2018/10/15 Python
python中类的属性和方法介绍
2018/11/27 Python
python并发编程 Process对象的其他属性方法join方法详解
2019/08/20 Python
pygame实现弹球游戏
2020/04/14 Python
南非领先的在线旅行社:Travelstart南非
2016/09/04 全球购物
Maisons du Monde德国:法国家具和装饰的市场领导者
2019/07/26 全球购物
英国景点门票网站:attractiontix
2019/08/27 全球购物
初中生自我评价
2014/02/01 职场文书
会计电算化毕业生自荐信
2014/03/03 职场文书
新闻专业毕业生求职信
2014/08/08 职场文书
python中%格式表达式实例用法
2021/06/18 Python
MySQL 如何限制一张表的记录数
2021/09/14 MySQL
python Tkinter模块使用方法详解
2022/04/07 Python