![python requests库网页爬取小实例:亚马逊商品页面的爬取 python requests库网页爬取小实例:亚马逊商品页面的爬取](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
由于直接通过requests.get()方法去爬取网页,它的头部信息的user-agent显示的是python-requests/2.21.0,所以亚马逊网站可能会拒绝访问。所以我们要更改访问的头部信息以对网站进行访问,更改头部信息模拟浏览器访问。
#亚马逊商品页面的爬取
import requests
url="https://www.amazon.cn/dp/B07GVXHCXH"
try:
kv={'user-agent':'Mozilla/5.0'}
r=requests.get(url,headers=kv)
r.raise_for_status()
r.encoding=r.apparent_encoding
print(r.text[1000:2000])
except:
print("爬取失败")