色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

Python如何使用隊(duì)列方式實(shí)現(xiàn)多線程爬蟲

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

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

代碼如下

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/' } # 實(shí)例化三個(gè)隊(duì)列,用來存放內(nèi)容 self.url_queue = Queue() self.html_queue = Queue() self.content_queue = Queue() def get_total_url(self): ''' 獲取了所有的頁面url,并且返回url_list return:url_list 現(xiàn)在放入url_queue隊(duì)列中保存 ''' 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隊(duì)列 self.url_queue.put(url_temp.format(i)) def parse_url(self): ''' 發(fā)送請(qǐng)求,獲取響應(yīng),同時(shí)etree處理html ''' while self.url_queue.not_empty: # 判斷非空,為空時(shí)結(jié)束循環(huán) # 從隊(duì)列中取出一個(gè)url url = self.url_queue.get() print('parsing url:',url) # 發(fā)送請(qǐng)求 response = requests.get(url,headers=self.headers,timeout=10) # 獲取html字符串 html = response.content.decode() # 獲取element類型的html html = etree.HTML(html) # 將生成的element對(duì)象放入html_queue隊(duì)列 self.html_queue.put(html) # Queue.task_done() 在完成一項(xiàng)工作之后,Queue.task_done()函數(shù)向任務(wù)已經(jīng)完成的隊(duì)列發(fā)送一個(gè)信號(hào) self.url_queue.task_done() def get_content(self): ''' 解析網(wǎng)頁內(nèi)容,獲取想要的信息 ''' 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的時(shí)候,隊(duì)列計(jì)數(shù)減一 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) # 發(fā)送網(wǎng)絡(luò)請(qǐng)求 for i in range(10): thread_parse = threading.Thread(target=self.parse_url) thread_list.append(thread_parse) # 提取數(shù)據(jù) 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: # 為每個(gè)進(jìn)程設(shè)置為后臺(tái)進(jìn)程,效果是主進(jìn)程退出子進(jìn)程也會(huì)退出 t.setDaemon(True) t.start()# 讓主線程等待,所有的隊(duì)列為空的時(shí)候才能退出 self.url_queue.join() self.html_queue.join() self.content_queue.join()if __name__=='__main__': obj = Qsbk() obj.run()

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 久久亚洲国产成人影院 | 综合色久七七综合七七蜜芽 | 精品在线免费观看 | 亚洲国产精久久久久久久 | 日本高清不卡中文字幕 | 特级黄色毛片在放 | 国产大陆亚洲精品国产 | 男人免费看片 | 特级aaa片毛片免费观看 | 日本韩国台湾香港三级 | 国产日韩亚洲欧美 | 亚洲精品毛片久久久久久久 | 国产步兵社区视频在线观看 | 国产日韩精品欧美一区 | 九九在线免费视频 | 日韩激情中文字幕一区二区 | 日韩欧美一区二区久久 | 国产福利一区二区在线精品 | 男女生性毛片免费观看 | 加勒比色久综合在线 | 杨幂丝袜国产福利视频 | 狼人激情网| 国产精品美女视视频专区 | 欧洲亚洲一区二区三区 | 久草资源在线视频 | 久久免费精彩视频 | 天堂va欧美ⅴa亚洲va一国产 | 999国产精品亚洲77777 | 午夜影院0606| 国产一区二区三区免费在线观看 | www.日本在线观看 | 国产成人lu在线视频 | 久久国产精品免费视频 | 国产99视频免费精品是看6 | 欧美另类色 | 久久精品国产99国产精品免费看 | 日韩午夜 | 国内三级视频 | 免费的三级网站 | 国产精品三级一区二区 | 美女黄色在线观看 |