Python如何使用队列方式实现多线程爬虫
作者:Norni 发布时间:2022-03-24 08:56:51
标签:Python,队列,多线程,爬虫
说明:糗事百科段子的爬取,采用了队列和多线程的方式,其中关键点是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()
来源:https://www.cnblogs.com/nuochengze/p/12861358.html
0
投稿
猜你喜欢
- 本文实例为大家分享了python实现图片中文字分割的具体代码,供大家参考,具体内容如下1、原始图片(包含数字):结果图:2、原始图片(包含文
- 网站 首页页面宽度 px Yahoo! 950 淘宝 950 MySpace 960 新浪 950 网易 960 Live Search 9
- 在整个产品设计的过程中,视觉设计与交互设计的“工序”非常紧密,两者关系也是相辅相成,互相影响。而视觉界面作为最直接与用户交流的层面,如何把交
- 现代网络应用Web APP或大型网站的后台一般只有一个,然后客户端却是各种各样的(iOS, android, 浏览器),
- 如果你在文件夹里有很多视频,并且文件夹里还有文件夹,文件夹里的文件夹也有视频,怎么能逐个读取并且保存。。所以我写了个代码用了os,walk,
- 可变长参数GO语言允许一个函数把任意数量的值作为参数,GO语言内置了**...操作符,在函数的最后一个形参才能使用...**操作符,使用它必
- 之前用来整理图片的小程序,拿来备忘,算是使用Python复制文件的一个例子。# -*- coding: utf-8 -*-#程序用来拷贝文件
- 前段时间在开发雨哲树网程序的时候,遇到需要转换地址中的参数,需要用到简单可逆运算的加密功能。在网上找了很多都不理想。因为我需要的这个可逆运算
- 这篇文章主要介绍了Python argparse模块使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
- 前言人类都是视觉动物,不管是男生还是女生看到漂亮的小姐姐、小哥哥就想截图保存下来。可是截图会对画质会产生损耗,截取的画面不规整,像素不高等问
- python的便利性,使得如今许多软件开发者、黑客都开始使用python打包成exe的方式进行程序的发布,这类exe有个特点,就是可以使用反
- 初识OpenCVOpenCV是一个开源的,跨平台的计算机视觉库,它采用优化的C/C++代码编写,能够充分利用多核处理器的优势,提供了Pyth
- 单例模式(Singleton Pattern) 是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在。当你希望在整个系统
- 一、Servlet实现文件上传,需要添加第三方提供的jar包下载地址:1) commons-fileupload-1.2.2-bin.zip
- 下面给大家分享Python爬虫后获取重定向url的两种方法,具体内容如下所示;方法(一)# 获得重定向url from urllib imp
- 最近有一个小项目,有如下的需求:将某几个源码文件夹进行打包,文件夹内有py文件、dll文件、exe文件等各种文件类型打包生成的安装包,在进行
- 导入在阅读过程中如果遇到一些带有水印的资料是比较烦心的,如下图所示,水印以及类似的内容会影响我们的阅读体验,而市面上去水印的功能有多要收费且
- Python作为一种功能强大的编程语言,因其简单易学而受到很多开发者的青睐。那么,Python 的应用领域有哪些呢?概括起来,Python的
- python3用到2个库import itertoolsimport metacomm.combinatorics.all_pairs2 a
- 今天在推上看到一条获取PHP类私有属性的推文,感觉很有意思:顺着推文联想,还有其他方式吗?经过自己的测试及网上答案,总结出三种方法:1. 反