一、京东商品页面爬取
import requests
url = "https://item.jd.com/2967929.html"
try:
r= requests.get(url)
r.raise_for_status()
r.encoding = r.apparent_encodin
print(r.text[:1000])
except:
print("爬取失败")
二、亚马逊商品爬取
检查返回状态码为503
检查编码 r.encoding 'ISO-8859-1'
改成可阅读编码r.encoding = r.apparent_encoding
查看requests头部信息 r.requests.header
更改头部信息:
kv = {'user-agent':'Mozilla/5.0'}
url = "https://www.amazon.cn/gp/product/B01M8L5Z3Y"
r = requests.get(url,header = kv)
r.status_code 200
全部代码:
import requests
url = "https://www.amazon.cn/gp/product/B01M8L5Z3Y"
try:
kv = {'user-agent':'Mozilla/5.0'}
r = requests.get(url,headers = kv)
r.raise_for_status()
r.encoding =