案例:腾牛网图片抓取
源代码如下:
import requests
url = 'https://www.qqtn.com/wm/meinvtp_1.html'
headers = {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}
res = requests.get(url, headers=headers
data = res.content.decode()
print(data)
执行之后,报错如下:
解决办法:
data = res.content.decode('GBK')
print(data)
运行结果如下:
# 自动获取解码格式
res.encoding = res.apparent_encoding
data = res.text
print(data)