今天要實作的是《爬蟲120例》中的第28例,采用的技術方案為多執行緒+佇列,
目標站點分析
本次要抓取的目標站點為:中介網,這個網站提供了網站排行榜、互聯網網站排行榜、中文網站排行榜等資料,
網站展示的樣本資料量是 :58341,
采集頁面地址為 https://www.zhongjie.com/top/rank_all_1.html,UI如下所示:

由于頁面存在一個【尾頁】超鏈接,所以直接通過該超鏈接獲取累計頁面即可,
其余頁面遵循簡單分頁規則:
https://www.zhongjie.com/top/rank_all_1.html
https://www.zhongjie.com/top/rank_all_2.html
基于此,本次Python爬蟲的解決方案如下,頁面請求使用 requests 庫,頁面決議使用 lxml,多執行緒使用 threading 模塊,佇列依舊采用 queue 模塊,
編碼時間
在正式編碼前,先通過一張圖將邏輯進行梳理,
本爬蟲撰寫步驟文字描述如下:
- 預先請求第一頁,決議出總頁碼;
- 通過生產者不斷獲取域名詳情頁地址,添加到佇列中;
- 消費者函式從佇列獲取詳情頁地址,決議目標資料,

總頁碼的生成代碼非常簡單
def get_total_page():
# get_headers() 函式,可參考開源代碼分享資料
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("//a[@class='weiye']/@href")[0]
pattern = re.compile('(\d+)')
page = pattern.search(last_page)
return int(page.group(1))
總頁碼生成完畢,就可以進行多執行緒相關編碼,本案例未撰寫存盤部分代碼,留給你自行完成啦,完整代碼如下所示:
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("//a[@class='weiye']/@href")[0]
pattern = re.compile('(\d+)')
page = pattern.search(last_page)
return int(page.group(1))
# 生產者
def producer():
while True:
# 取一個分類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)
# 消費者
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)
# 資料提取,更多資料提取,可自行撰寫 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__":
# 初始化一個佇列
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個生產者執行緒
for p_in in range(1, 3):
p = threading.Thread(target=producer)
p.start()
# 開啟2個消費者執行緒
for p_in in range(1, 2):
p = threading.Thread(target=consumer)
p.start()
收藏時間
代碼倉庫地址:https://codechina.csdn.net/hihell/python120,去給個關注或者 Star 吧,
資料沒有采集完畢,想要的可以在評論區留言交流
今天是持續寫作的第 210 / 365 天,
可以關注,點贊、評論、收藏,
更多精彩
- Python 爬蟲 100 例教程導航帖(已完結,復盤更新中,目前 110+ 篇)

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/297589.html
標籤:python
