網絡爬蟲是一種自動抓取互聯網信息的腳本程序,廣泛應用于搜索引擎、數據分析和內容聚合。這次我將帶大家使用Python快速構建一個基礎爬蟲,為什么使用python做爬蟲?主要就是支持的庫很多,而且同類型查詢文檔多,在同等情況下,使用python做爬蟲,成本、時間、效率等總體各方便綜合最優的選擇。廢話不多說直接開干。
環境準備
pip install requests beautifulsoup4 # 安裝核心庫
基礎爬蟲四步法
1. 發送HTTP請求
import requestsurl = "https://example.com"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}response = requests.get(url, headers=headers)
print(f"狀態碼: {response.status_code}") # 200表示成功
2. 解析HTML內容
from bs4 import BeautifulSoupsoup = BeautifulSoup(response.text, 'html.parser')# 提取標題
title = soup.title.text
print(f"頁面標題: {title}")# 提取所有鏈接
links = [a['href'] for a in soup.find_all('a', href=True)]
print(f"發現{len(links)}個鏈接")
3. 數據存儲
# 存儲到CSV文件
import csvwith open('data.csv', 'w', newline='', encoding='utf-8') as f:writer = csv.writer(f)writer.writerow(['標題', '鏈接'])for link in links:writer.writerow([title, link])
4. 處理分頁
base_url = "https://example.com/page/{}"
for page in range(1, 6): # 爬取5頁page_url = base_url.format(page)response = requests.get(page_url)# 解析和存儲邏輯...
高級技巧
1. 處理動態內容(使用Selenium)
from selenium import webdriverdriver = webdriver.Chrome()
driver.get("https://dynamic-site.com")
dynamic_content = driver.page_source
# 后續解析過程相同
driver.quit()
2. 避免被封禁
import time
import random# 隨機延遲(1-3秒)
time.sleep(random.uniform(1, 3))# 使用代理IP
proxies = {"http": "http://10.10.1.10:3128"}
requests.get(url, proxies=proxies)
3. 遵守robots.txt
from urllib.robotparser import RobotFileParserrp = RobotFileParser()
rp.set_url("https://example.com/robots.txt")
rp.read()
if rp.can_fetch("*", url):# 允許爬取
完整示例:爬取圖書信息
import requests
from bs4 import BeautifulSoupurl = "http://books.toscrape.com/"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')books = []
for book in soup.select('article.product_pod'):title = book.h3.a['title']price = book.select_one('p.price_color').textbooks.append((title, price))print(f"抓取到{len(books)}本書籍")
for title, price in books[:3]:print(f"- {title}: {price}")
重要提醒
1、法律合規:遵守網站robots.txt
協議,不爬取敏感數據
2、頻率控制:添加延遲避免對服務器造成壓力
3、異常處理:添加try-except應對網絡錯誤
try:response = requests.get(url, timeout=5)
except requests.exceptions.RequestException as e:print(f"請求失敗: {e}")
4、User-Agent輪換:使用不同瀏覽器標
通過上面這個教程,想必大家已經掌握了爬蟲的基本原理和實現方法。實際開發中可根據需求添加數據庫存儲、異步處理等高級功能,當然這個是后續學習的范疇,也是更高要求爬蟲項目必會的環節。