網(wǎng)站首頁 編程語言 正文
python利用多線程+隊(duì)列技術(shù)爬取中介網(wǎng)互聯(lián)網(wǎng)網(wǎng)站排行榜_python
作者:??夢想橡皮擦???? ? 更新時(shí)間: 2022-07-11 編程語言目標(biāo)站點(diǎn)分析
本次要抓取的目標(biāo)站點(diǎn)為:中介網(wǎng),這個(gè)網(wǎng)站提供了網(wǎng)站排行榜、互聯(lián)網(wǎng)網(wǎng)站排行榜、中文網(wǎng)站排行榜等數(shù)據(jù)。
網(wǎng)站展示的樣本數(shù)據(jù)量是 :58341。
采集頁面地址為?https://www.zhongjie.com/top/rank_all_1.html
,
UI如下所示:?
?由于頁面存在一個(gè)【尾頁】超鏈接,所以直接通過該超鏈接獲取累計(jì)頁面即可。
其余頁面遵循簡單分頁規(guī)則:
https://www.zhongjie.com/top/rank_all_1.html https://www.zhongjie.com/top/rank_all_2.html
基于此,本次Python爬蟲的解決方案如下,頁面請求使用?requests
?庫,頁面解析使用?lxml
,多線程使用?threading
?模塊,隊(duì)列依舊采用?queue
?模塊。
編碼時(shí)間
在正式編碼前,先通過一張圖將邏輯進(jìn)行梳理。
本爬蟲編寫步驟文字描述如下:
- 預(yù)先請求第一頁,解析出總頁碼;
- 通過生產(chǎn)者不斷獲取域名詳情頁地址,添加到隊(duì)列中;
- 消費(fèi)者函數(shù)從隊(duì)列獲取詳情頁地址,解析目標(biāo)數(shù)據(jù)。
總頁碼的生成代碼非常簡單
def get_total_page(): # get_headers() 函數(shù),可參考開源代碼分享數(shù)據(jù) res = requests.get( 'https://www.zhongjie.com/top/rank_all_1.html', headers=get_headers(), timeout=5) element = etree.HTML(res.text) last_page = element.xpath("http://a[@class='weiye']/@href")[0] pattern = re.compile('(\d+)') page = pattern.search(last_page) return int(page.group(1))
總頁碼生成完畢,就可以進(jìn)行多線程相關(guān)編碼,本案例未編寫存儲部分代碼,留給你自行完成啦,
完整代碼如下所示:
from queue import Queue import time import threading import requests from lxml import etree import random import re def get_headers(): uas = [ "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)", "Mozilla/5.0 (compatible; Baiduspider-render/2.0; +http://www.baidu.com/search/spider.html)" ] ua = random.choice(uas) headers = { "user-agent": ua } return headers def get_total_page(): res = requests.get( 'https://www.zhongjie.com/top/rank_all_1.html', headers=get_headers(), timeout=5) element = etree.HTML(res.text) last_page = element.xpath("http://a[@class='weiye']/@href")[0] pattern = re.compile('(\d+)') page = pattern.search(last_page) return int(page.group(1)) # 生產(chǎn)者 def producer(): while True: # 取一個(gè)分類ID url = urls.get() urls.task_done() if url is None: break res = requests.get(url=url, headers=get_headers(), timeout=5) text = res.text element = etree.HTML(text) links = element.xpath('//a[@class="copyright_title"]/@href') for i in links: wait_list_urls.put("https://www.zhongjie.com" + i) # 消費(fèi)者 def consumer(): while True: url = wait_list_urls.get() wait_list_urls.task_done() if url is None: break res = requests.get(url=url, headers=get_headers(), timeout=5) text = res.text element = etree.HTML(text) # 數(shù)據(jù)提取,更多數(shù)據(jù)提取,可自行編寫 xpath title = element.xpath('//div[@class="info-head-l"]/h1/text()') link = element.xpath('//div[@class="info-head-l"]/p[1]/a/text()') description = element.xpath('//div[@class="info-head-l"]/p[2]/text()') print(title, link, description) if __name__ == "__main__": # 初始化一個(gè)隊(duì)列 urls = Queue(maxsize=0) last_page = get_total_page() for p in range(1, last_page + 1): urls.put(f"https://www.zhongjie.com/top/rank_all_{p}.html") wait_list_urls = Queue(maxsize=0) # 開啟2個(gè)生產(chǎn)者線程 for p_in in range(1, 3): p = threading.Thread(target=producer) p.start() # 開啟2個(gè)消費(fèi)者線程 for p_in in range(1, 2): p = threading.Thread(target=consumer) p.start()
原文鏈接:https://juejin.cn/post/7079964924385968135
相關(guān)推薦
- 2022-10-14 sklearn.linear_model.Perceptron詳解
- 2022-10-31 React中使用antd組件的方法_React
- 2022-08-27 Pycharm遠(yuǎn)程連接服務(wù)器跑代碼的實(shí)現(xiàn)_python
- 2022-04-25 一篇文章帶你了解C語言的文件操作_C 語言
- 2022-09-24 win2019?ftp服務(wù)器搭建圖文教程_FTP服務(wù)器
- 2022-11-03 ahooks?useVirtualList?封裝虛擬滾動列表_React
- 2023-01-19 python使用?f?格式化字符串的用法_python
- 2023-06-04 Pandas中MultiIndex選擇并提取任何行和列_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支