问题描述
每当我想在 amazon.com 上抓取时,我都失败了。因为产品信息会根据 amazon.com 中的位置而变化
这个变化的信息如下;
- 1-价格
- 2-运费
- 3-海关费用
- 4-发货状态
用selenium改变位置很简单,但是处理速度很慢。所以这就是为什么我需要使用scrapy或requests进行抓取。
然而,虽然我在浏览器中模仿了 cookie 和 headers,但 amazon.com 不允许我更改位置。
有两个大问题。
- 有一个数据叫做“ubid-main”,我无法导出 这个数据。这是没有数据的亚马逊。它不允许改变 位置。
- 虽然我对header数据做了同样的处理,但是还是有区别的 传出数据之间。示例:我使用完全相同的标题 浏览器。但是在浏览器中,Content-Type 是 json,但是 在我制作的代码中,它是 text/html;字符集 = UTF-8 编码。
而且很有趣的是没有关于这个主题的信息。您无法对世界第一的购物网站进行面向位置的抓取。
请知道这个主题的答案的人赐教。 如果有scrapy或requests这样的解决方案就足够了。 说真的,我已经 1 年没有解决这个问题了。
import requests
from lxml import etree
from random import choice
from urllib3.exceptions import InsecureRequestWarning
import urllib.parse
import urllib3.request
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def location():
headersdelivery = {
'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/88.0.4324.190 Safari/537.36','content-type':'application/x-www-form-urlencoded','accept':'text/html,*/*','x-requested-with':'XMLHttpRequest','contenttype':'application/x-www-form-urlencoded;charset=utf-8','origin':'https://www.amazon.com','sec-fetch-site':'same-origin','sec-fetch-mode':'cors','sec-fetch-dest':'empty','referer':'https://www.amazon.com/','accept-encoding':'gzip,deflate,br','accept-language':'tr-TR,tr;q=0.9,en-US;q=0.8,en;q=0.7'
}
payload = {
'locationType':'LOCATION_INPUT','zipCode':'34249','storeContext':'generic','deviceType':'web','pageType':'Gateway','actionSource':'glow','almBrandId':'undefined'}
sessionid = requests.session()
url = "https://www.amazon.com/gp/delivery/ajax/address-change.html"
ulkesecmereq = sessionid.post(url,headers=headersdelivery,data=payload,verify=False)
return sessionid
def response(locationsession):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Language': 'en-US,en;q=0.5','Accept-Encoding': 'gzip,'Connection': 'keep-alive','Upgrade-Insecure-Requests': '1','TE': 'Trailers'}
postdata = {
'storeContext':'generic','pageType':'Gateway'
}
req = locationsession.post("https://www.amazon.com/gp/glow/get-location-label.html",headers=headers,data=postdata,verify=False)
print(req.content)
locationsession = location()
response(locationsession)
解决方法
我在您的位置请求中遗漏的标头中看到了 CSRF 令牌(anti-csrftoken-a2z),还有一个遗漏了对该位置的其他请求(https://www.amazon.co.uk/gp/glow/get-address-selections.html?deviceType=desktop&pageType=Gateway&storeContext=NoStoreName&actionSource=desktop-modal)。您应该像在浏览器中一样实现所有请求。
Chrome 中的简单示例:
Chrome -> devtools -> network -> XHR
copy as curl
在此处复制并转换为请求库 (https://curl.trillworks.com/)。