在Python中,我們可以使用threading
庫來實現多線程爬蟲。以下是一個簡單的多線程爬蟲示例,使用了requests
和BeautifulSoup
庫來爬取網頁內容。
首先,確保已經安裝了所需的庫:
pip install requests
pip install beautifulsoup4
然后,編寫多線程爬蟲代碼:
import requests
from bs4 import BeautifulSoup
import threading
# 爬取目標URL
def fetch(url):
response = requests.get(url)
if response.status_code == 200:
return response.text
else:
print(f"Error fetching {url}: Status code {response.status_code}")
return None
# 解析網頁內容并提取數據
def parse(html):
soup = BeautifulSoup(html, "html.parser")
# 在這里提取你需要的數據,例如:
titles = soup.find_all("h2", class_="title")
for title in titles:
print(title.get_text())
# 爬蟲線程函數
def crawl_thread(url):
html = fetch(url)
if html:
parse(html)
# 主程序
if __name__ == "__main__":
urls = [
"https://example.com/page1",
"https://example.com/page2",
"https://example.com/page3",
# 添加更多目標URL
]
threads = []
for url in urls:
thread = threading.Thread(target=crawl_thread, args=(url,))
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
這個示例中,我們定義了三個函數:fetch
用于發送HTTP請求并獲取網頁內容,parse
用于解析HTML并提取數據,crawl_thread
作為爬蟲線程函數,用于執行fetch
和parse
操作。
在主程序中,我們創建了一個URL列表,并為每個URL創建一個線程。然后,我們啟動所有線程并等待它們完成。
請注意,這個示例僅用于演示目的。在實際應用中,你可能需要根據目標網站的結構和反爬蟲策略進行相應的調整。同時,為了避免對目標網站造成過大壓力,建議使用多進程(multiprocessing
庫)或異步編程(asyncio
庫)來實現爬蟲。