logo
Loading...

使用requests.get讀取網頁出現亂碼 - Cupoy

import requestsurl = 'https://www.zhihu.com/explor...

pycrawler

使用requests.get讀取網頁出現亂碼

2019/12/14 05:08 AM
Python網路爬蟲論壇
Lin Chun Yu
觀看數:0
回答數:1
收藏數:0
pycrawler

import requests
url
= 'https://www.zhihu.com/explore'

'''
上面結果回傳400,因為沒有給headers
'''
headers
= {
   
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
   
'accept-encoding': 'gzip, deflate, br',
   
'accept-language': 'zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-CN;q=0.6,ja;q=0.5',
   
'cache-control': 'max-age=0',
   
'cookie': '_zap=a5634a47-a806-4828-8ab4-2cbc03734bda; d_c0="AFBoGjc1fg6PTus2_H76YgMM3xvztVHRnCs=|1541767835"; __gads=ID=18e1196642fc5994:T=1544975081:S=ALNI_MZ2lQjiLHPlLxjVqGGH6o-EiL2luQ; z_c0="2|1:0|10:1551204934|4:z_c0|92:Mi4xM1FkMERnQUFBQUFBVUdnYU56Vi1EaVlBQUFCZ0FsVk5SdEJpWFFDUXZ6REVHVWw0QWtXblh6WGt4T183ekdxNFhn|fa542d11758c34207cdd6a1edf85de768ac02624c8cb53aa687017af49accd7b"; tst=r; q_c1=aed50b9b158344d6ac78a230c8970d83|1560948516000|1543510922000; __utmv=51854390.100--|2=registration_date=20190226=1^3=entry_date=20181130=1; _xsrf=13cd498d-6139-4c50-99a6-df5c749dd64c; tgw_l7_route=4860b599c6644634a0abcd4d10d37251; __utma=51854390.567487929.1560948518.1560950333.1562469680.3; __utmb=51854390.0.10.1562469680; __utmc=51854390; __utmz=51854390.1562469680.3.3.utmcsr=localhost:8888|utmccn=(referral)|utmcmd=referral|utmcct=/notebooks/day2-example.ipynb',
   
'referer': 'http://localhost:8888/notebooks/day2-example.ipynb',
   
'upgrade-insecure-requests': '1',
   
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
}
r
= requests.get(url, headers = headers)

r
.encoding = 'utf-8'
print(r.text[0:600])


以上程式碼跑完後,結果是亂碼,請問要如何解決?

謝謝!