模擬用戶代理 (User Agent) 存取目標網頁內容

Steven Wang
Jul 22, 2017 · 2 min read

騙過網路伺服器,成功存取內容

網頁本身為避免被Google或惡意爬蟲軟體取得網頁資訊,網頁會檢查代理服務User Agent
若檢查不是來自一般瀏覽器,就不會讓去存取到頁面內容
我們模擬代理服務才可以存取

檢查>Network>Headers>Request Headers>User-Agent

import requests
head = {‘User-Agent’:’Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'} #字典方式{'User-Agent':’資料’}
res = requests.get(‘https://www.yungching.com.tw/',headers=head)
print(res.text)

Reference From 大數學堂

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade