协程池爬虫
# 线程池和协程池实现爬虫
# 学习目标
- 掌握 协程池模块的使用
- 掌握 使用线程池实现爬虫的流程
# 1 协程池模块使用介绍
- 协程池模块
import gevent.monkey
gevent.monkey.patch_all()
from gevent.pool import Pool
1
2
3
2
3
# 2 使用协程池实现爬虫的具体实现过程
# coding=utf-8
import gevent.monkey
gevent.monkey.patch_all()
from gevent.pool import Pool
import requests
from lxml import etree
from queue import Queue
import time
class QiubaiSpider:
def __init__(self):
self.url_temp = "https://www.qiushibaike.com/8hr/page/{}/"
self.headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X \
10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"}
self.queue = Queue()
self.pool = Pool(5)
self.is_running = True
self.total_requests_num = 0
self.total_response_num = 0
def get_url_list(self): # 获取url列表
for i in range(1, 14):
self.queue.put(self.url_temp.format(i))
self.total_requests_num += 1
def parse_url(self, url): # 发送请求,获取响应
return requests.get(url, headers=self.headers).content.decode()
def get_content_list(self, html_str): # 提取段子
html = etree.HTML(html_str)
div_list = html.xpath("//div[@id='content-left']/div")
content_list = []
for div in div_list:
content = {}
content["content"] = div.xpath(".//div[@class='content']/span/text()")
print(content)
content_list.append(content)
return content_list
def save_content_list(self, content_list): # 保存数据
for content in content_list:
print(content) # 此处对数据进行保存操作
def exetute_requests_item_save(self):
url = self.queue.get()
html_str = self.parse_url(url)
content_list = self.get_content_list(html_str)
self.save_content_list(content_list)
self.total_response_num += 1
def _callback(self, temp):
if self.is_running:
self.pool.apply_async(self.exetute_requests_item_save, callback=self._callback)
def run(self):
self.get_url_list()
for i in range(2): # 控制并发
self.pool.apply_async(self.exetute_requests_item_save, callback=self._callback)
while True: # 防止主线程结束
time.sleep(0.0001) # 避免cpu空转,浪费资源
if self.total_response_num >= self.total_requests_num:
self.is_running = False
break
if __name__ == '__main__':
qiubai = QiubaiSpider()
qiubai.run()
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 小结
- 协程池的导包:
from gevent.pool import Pool
- 协程池异步方法:
pool.apply_async()
编辑 (opens new window)