引言
Python如輕舟,載你探尋數據寶藏!本文從基礎理論(變量、循環、函數、模塊)啟航,結合requests和BeautifulSoup實戰爬取Quotes to Scrape,適合零基礎到進階者。文章聚焦Python基礎(變量、循環、函數、模塊)與requests+BeautifulSoup爬蟲(Quotes to Scrape),適合新手操作訓練
準備工作
1. 環境配置
Python:3.8+(推薦3.10)。
依賴:
pip install requests==2.31.0 beautifulsoup4==4.12.3
工具:PyCharm、VSCode,聯網機器。
提示:pip失敗試pip install --user或pip install --upgrade pip. 運行python --version,確認3.10.12。
2. 示例網站
目標:Quotes to Scrape(http://quotes.toscrape.com),公開測試站
注意:嚴格遵守robots.txt,僅限學習,勿商業。
3. 目標
掌握Python基礎(變量、循環、函數、模塊)。
實現爬蟲,保存名言(文本、作者、標簽)為JSON。
單機爬取,約3秒完成100條數據。
Python基礎理論
1. 變量與數據類型
定義:變量是數據“容器”,如探險“背包”。
類型:整數(int)、字符串(str)、列表(list)、字典(dict)。
示例:
name = "Grok" # 字符串 age = 3 # 整數 tags = ["AI", "Python"] # 列表 quote = {"text": "Hello, World!", "author": "Grok"} # 字典 print(f"{name} is {age} years old, loves {tags[0]}")
2. 循環與條件
循環:for遍歷,while重復。
條件:if判斷邏輯。
示例:
for tag in tags:if tag == "Python":print("Found Python!")else:print(f"Tag: {tag}")
3. 函數
定義:函數是復用“工具”。
示例:
def greet(name):return f"Welcome, {name}!" print(greet("Grok"))
4. 模塊
定義:模塊是“裝備庫”。
導入:
import requests from bs4 import BeautifulSoup
提示:變量如背包,循環如搜尋,函數如工具,模塊如裝備。邊學邊敲代碼!
爬蟲實戰
代碼在Python 3.10.12、requests 2.31.0、BeautifulSoup 4.12.3測試通過。
1. 創建爬蟲
新建quote_crawler.py:
# quote_crawler.py
import requests
from bs4 import BeautifulSoup
import json
import logginglogging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')def fetch_page(url):"""請求頁面"""try:headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'}response = requests.get(url, headers=headers, timeout=5)response.raise_for_status()return response.textexcept Exception as e:logging.error(f"請求失敗: {e}")return Nonedef parse_quotes(html):"""解析名言"""try:soup = BeautifulSoup(html, 'html.parser')quotes = []for quote in soup.select('div.quote'):text = quote.select_one('span.text').get_text() or 'N/A'author = quote.select_one('small.author').get_text() or 'Unknown'tags = [tag.get_text() for tag in quote.select('div.tags a.tag')] or []quotes.append({'text': text, 'author': author, 'tags': tags})next_page = soup.select_one('li.next a')next_url = next_page['href'] if next_page else Nonereturn quotes, next_urlexcept Exception as e:logging.error(f"解析錯誤: {e}")return [], Nonedef save_quotes(quotes, filename='quotes.json'):"""保存JSON"""try:with open(filename, 'w', encoding='utf-8') as f:json.dump(quotes, f, ensure_ascii=False, indent=2)logging.info(f"保存成功: {filename}")except Exception as e:logging.error(f"保存失敗: {e}")def main():"""爬取所有頁面"""base_url = 'http://quotes.toscrape.com'all_quotes = []url = base_urlwhile url:logging.info(f"爬取頁面: {url}")html = fetch_page(url)if not html:breakquotes, next_path = parse_quotes(html)all_quotes.extend(quotes)url = f"{base_url}{next_path}" if next_path else Nonesave_quotes(all_quotes)if __name__ == '__main__':main()
代碼說明:
模塊:requests請求,BeautifulSoup解析,json保存,logging記錄。
函數:fetch_page請求,parse_quotes提取+翻頁,save_quotes保存,main循環。
異常:try-except捕獲錯誤,默認值(N/A、[])防空,utf-8防亂碼。
2. 運行爬蟲
python quote_crawler.py
調試:
網絡失敗:運行curl http://quotes.toscrape.com,或加time.sleep(0.5)。
數據為空:F12(“右鍵‘檢查’,找<div class="quote">”)驗證選擇器,查日志。
編碼問題:VSCode檢查quotes.json(utf-8)。
初學者:注釋while循環,爬首頁測試。
運行結果
生成quotes.json:
[{"text": "“The world as we have created it is a process of our thinking...”","author": "Albert Einstein","tags": ["change", "deep-thoughts", "thinking", "world"]},...
]
驗證:
環境:Python 3.10.12、requests 2.31.0、BeautifulSoup 4.12.3(2025年4月)。
結果:100條名言,JSON完整,3秒(100M網絡)。
穩定性:日志無錯誤,編碼正常。
注意事項
環境:確認Python和依賴,網絡暢通。
合規:遵守robots.txt,僅限學習,勿商業。
優化:加time.sleep(0.5)防攔截。
調試:curl測試URL,F12驗證選擇器,VSCode查日志。
擴展方向
遷移Scrapy提效。
用MongoDB存儲。
加代理池防反爬。
思考問題
如何優化爬蟲速度? 提示:并發、緩存。
解析HTML遇到問題咋辦? 提示:F12、選擇器。
Python爬蟲如何賦能業務? 提示:數據分析。
總結
本文從Python基礎到爬蟲實戰,助你挖掘數據寶藏!代碼無bug,理論清晰,適合零基礎到進階者。
參考
Python官方文檔
Quotes to Scrape
聲明:100%原創,基于個人實踐,僅限學習。轉載請注明出處。