← 課程總覽

🕸️ 第7堂課:網頁爬蟲入門

程式學習第七堂課:Python requests 庫、HTTP 協定、BeautifulSoup HTML 解析、即時天氣查詢專案
含語音講解(約 60 分鐘),附互動小測驗

📖 第一階段:HTTP 基礎(約 20 分鐘)

1. 瀏覽器背後發生了什麼?

⏱ 10 分鐘

Python 也能扮演瀏覽器,這就是爬蟲的基礎:

# pip install requests
import requests
response = requests.get("https://jiu-tao.com")
print(response.status_code)  # 200 = 成功

2. requests 庫完整用法

⏱ 15 分鐘
# GET 請求
r = requests.get("https://api.example.com/data")
print(r.json())

# 帶參數
r = requests.get("https://api.example.com/search",
    params={"q": "Python", "page": 1})

# POST 請求
r = requests.post("https://api.example.com/login",
    json={"username": "ding"})

# 錯誤處理
try:
    r = requests.get("https://bad-site.com", timeout=5)
    r.raise_for_status()
except requests.exceptions.Timeout:
    print("請求超時")

📖 第二階段:HTML 解析(約 20 分鐘)

3. BeautifulSoup:HTML 解剖刀

⏱ 15 分鐘
# pip install beautifulsoup4
from bs4 import BeautifulSoup

soup = BeautifulSoup(r.text, "html.parser")
title = soup.find("title").text
all_links = soup.find_all("a")
for link in all_links[:5]:
    print(link.get("href"), link.text)

# CSS 選擇器
articles = soup.select("article h2")
prices = soup.select(".price")

📖 第三階段:實戰(約 20 分鐘)

4. 專案:即時天氣查詢

⏱ 15 分鐘
import requests

def get_weather(city="臺北"):
    url = "https://opendata.cwa.gov.tw/api/v1/rest/datastore/F-C0032-001"
    params = {"Authorization": "YOUR_API_KEY", "locationName": city}
    try:
        r = requests.get(url, params=params, timeout=10)
        r.raise_for_status()
        data = r.json()
        loc = data["records"]["location"][0]
        print(f"🌤️ {loc['locationName']} 天氣:")
        for wx in loc["weatherElement"]:
            if wx["elementName"] == "Wx":
                print(wx["time"][0]["parameter"]["parameterName"])
    except Exception as e:
        print(f"查詢失敗: {e}")
🌐 爬蟲禮儀:尊重 robots.txt、請求間加延遲、不要狂送請求。

🔍 HTTP 與 requests 深入

⏱ 12 分鐘
import requests

# GET 請求帶參數
params = {"q": "Python爬蟲", "page": 1}
r = requests.get("https://api.example.com/search", params=params)
print(r.url)  # https://api.example.com/search?q=Python爬蟲&page=1

# POST 請求送 JSON
data = {"name": "丁丁", "score": 95}
r = requests.post("https://api.example.com/users", json=data)

# 自訂 headers(模擬瀏覽器)
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
    "Accept-Language": "zh-TW,zh;q=0.9"
}
r = requests.get("https://example.com", headers=headers)

# 處理回應
print(r.status_code)     # 200 表示成功
if r.status_code == 200:
    data = r.json()      # JSON → Python dict
elif r.status_code == 404:
    print("頁面不存在")
🌐 HTTP 狀態碼:200 成功、404 不存在、500 伺服器錯誤
⚠️ 一定要加 User-Agent,否則很多網站會拒絕你的請求!

🔍 BeautifulSoup 實戰技巧

⏱ 10 分鐘
from bs4 import BeautifulSoup
import requests

url = "https://books.toscrape.com"
soup = BeautifulSoup(requests.get(url).text, "html.parser")

# CSS 選擇器(最推薦的方式)
titles = soup.select("h3 a")        # 所有書名連結
prices = soup.select(".price_color")  # class 選擇器

for title, price in zip(titles[:5], prices[:5]):
    print(f"{title['title']} — {price.text}")

# 常用搜尋方法
soup.find("h1")                     # 找第一個 h1
soup.find_all("a", class_="link")   # 找特定 class 的 a
soup.select("a[href*='catalogue']") # CSS: href 含 catalogue 的連結
📌 select()find() 更直覺
🛡️ 禮貌爬蟲:每次請求間隔 1-3 秒、檢查 robots.txt、不要打爆別人伺服器。

🧪 小測驗:你學會了嗎?

5 題選擇題,選完按「交卷」看成績
第 1 / 5 題
requests.get() status_code 200 代表?
第 2 / 5 題
BeautifulSoup 的主要功能?
第 3 / 5 題
soup.find_all("a") 回傳什麼?
第 4 / 5 題
為什麼設定 User-Agent header?
第 5 / 5 題
r.raise_for_status() 作用?
你的得分
0/5

🎯 第7堂課,你學會了什麼?

HTTP 請求requestsBeautifulSoup天氣查詢

你現在可以把整個網路當成你的資料庫了!

→ 前往第8堂課