国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Python如何使用隊列方式實現多線程爬蟲

瀏覽:98日期:2022-07-25 17:25:23

說明:糗事百科段子的爬取,采用了隊列和多線程的方式,其中關鍵點是Queue.task_done()、Queue.join(),保證了線程的有序進行。

代碼如下

import requestsfrom lxml import etreeimport jsonfrom queue import Queueimport threadingclass 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 Noneauthor_name = i.xpath('.//a[@rel=’nofollow’]/img/@alt')author_name = author_name[0] if len(author_name)>0 else Noneauthor_href = i.xpath('./a/@href')author_+author_href[0] if len(author_href)>0 else Noneauthor_gender = i.xpath('./div[1]/div/@class')author_gender = author_gender[0].split(' ')[-1].replace('Icon','').strip() if len(author_gender)>0 else Noneauthor_age = i.xpath('./div[1]/div/text()')author_age = author_age[0] if len(author_age)>0 else Nonecontent = i.xpath('./a/div/span/text()')content = content[0].strip() if len(content)>0 else Nonecontent_vote = i.xpath('./div[@class=’stats’]/span[@class=’stats-vote’]/i/text()')content_vote = content_vote[0] if len(content_vote)>0 else Nonecontent_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 Noneitem = { '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 編程
相關文章:
主站蜘蛛池模板: 天柱县| 宁安市| 土默特右旗| 恩施市| 五华县| 屯门区| 越西县| 黔西县| 牙克石市| 德令哈市| 常熟市| 仙居县| 得荣县| 嫩江县| 仲巴县| 长垣县| 清苑县| 清镇市| 太仓市| 贺兰县| 开化县| 巴塘县| 普洱| 溧阳市| 乌鲁木齐县| 当雄县| 老河口市| 云阳县| 横峰县| 登封市| 通山县| 如皋市| 文化| 苗栗县| 庄浪县| 五莲县| 佛学| 上犹县| 宁晋县| 大余县| 沅陵县|