多线程爬虫

本文分享了一个糗事百科的爬虫项目实现细节,利用Python的requests库和lxml库,通过多线程的方式,实现了糗事百科网页的批量抓取、解析和数据存储。项目中,作者详细介绍了如何构建URL列表、发送请求、解析HTML并提取关键信息,最后将数据进行存储。

 

糗事百科

 1 import requests
 2 from lxml import etree
 3 from queue import Queue
 4 import threading
 5 
 6 
 7 class Qiubai:
 8     def __init__(self):
 9         self.temp_url = "https://www.qiushibaike.com/8hr/page/{}/"
10         self.headers= {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X \
11         10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"}
12         self.url_queue = Queue()
13         self.html_queue = Queue()
14         self.content_list_queue = Queue()
15 
16     def get_url_list(self):#获取url列表
17         for i in range(1,14):
18             self.url_queue.put(self.temp_url.format(i))
19 
20     def parse_url(self):
21         while True: #在这里使用,子线程不会结束,把子线程设置为守护线程
22             url = self.url_queue.get()
23             # print(url)
24             response = requests.get(url,headers=self.headers)
25             self.html_queue.put(response.content.decode())
26             self.url_queue.task_done()
27 
28 
29     def get_content_list(self):  #提取数据
30         while True:
31             html_str = self.html_queue.get()
32             html = etree.HTML(html_str)
33             div_list = html.xpath("//div[@id='content-left']/div")
34             content_list = []
35             for div in div_list:
36                 content = {}
37                 content["content"] = div.xpath(".//div[@class='content']/span/text()")
38                 content_list.append(content)
39             self.content_list_queue.put(content_list)
40             self.html_queue.task_done()
41 
42     def save_content_list(self):
43         while True:
44             content_list = self.content_list_queue.get()
45             for content in content_list:
46                 print(content) # 此处对数据进行保存操作
47             self.content_list_queue.task_done()
48 
49 
50     def run(self):
51         thread_list = []
52         #1.url_list
53         t_url = threading.Thread(target=self.get_url_list)
54         thread_list.append(t_url)
55         #2.遍历,发送请求,
56         for i in range(3):  #三个线程发送请求
57             t_parse = threading.Thread(target=self.parse_url)
58             thread_list.append(t_parse)
59         #3.提取数据
60         t_content = threading.Thread(target=self.get_content_list)
61         thread_list.append(t_content)
62         #4.保存
63         t_save = threading.Thread(target=self.save_content_list)
64         thread_list.append(t_save)
65 
66         for t in thread_list:
67             t.setDaemon(True)  #把子线程设置为守护线程,当前这个线程不重要,主线程结束,子线程技术
68             t.start()
69 
70         for q in [self.url_queue,self.html_queue,self.content_list_queue]:
71             q.join()  #让主线程阻塞,等待队列的计数为0,
72 
73         print("主线程结束")
74 
75 if __name__ == '__main__':
76 
77     qiubai = Qiubai()
78     qiubai.run()

 

转载于:https://www.cnblogs.com/wanglinjie/p/9310329.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值