import requests
url='https://www.google.com.tw/search'
keyword=input('輸入關鍵詞')
try:
kv={'q':keyword}
ev={'user-agent':'Mozilla/5.0'}
r=requests.get(url,params=kv,headers=ev)
print(r.request.url)
r.enconding=r.apparent_encoding
r.raise_for_status()
print(len(r.text))
except:
print('爬取失敗')
試試看這樣吧:
import requests
url = 'https://www.google.com.tw/search'
keyword = input('輸入關鍵詞:')
# Setup parameters.
kv = {'q': keyword}
ev = {'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0'}
try:
# Make a request.
r = requests.get(url, params=kv, headers=ev)
# Print request url.
print(r.request.url)
# Decode returned text.
r.enconding = r.apparent_encoding
r.raise_for_status()
print('Length :', len(r.text))
# Output returned text to file.
with open('output.html', 'w') as fout:
fout.write(r.text)
except:
print('爬取失敗')
感覺只是 user-agent 的問題! Mozilla 5.0 似乎太老了吧!