python 亚马逊刮-刮工程有时

qni6mghb  于 2023-01-16  发布在  Python
关注(0)|答案(2)|浏览(120)

我从amazon抓取数据用于教育目的,我的cookie和反机器人程序有一些问题,我设法抓取了数据,但有时,cookie不在响应中,或者反机器人程序标记了我。
我已经尝试使用一个随机的标题列表,如下所示:

headers_list = [{
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
    "Accept-Language": "en-US,en;q=0.5",
    "DNT": "1",
    "Connection": "keep-alive",
    "Upgrade-Insecure-Requests": "1",
    "Sec-Fetch-User": "?1",
    "TE": "trailers"
},
    {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
    "Accept-Language": "fr-FR,fr;q=0.7",
    "cache-control": "max-age=0",
    "content-type": "application/x-www-form-urlencoded",
    "sec-fetch-dest": "document",
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "same-origin",
    "sec-fetch-user": "?1",
    "upgrade-insecure-requests": "1"
    },
]

并将以下代码放入我的代码中:

headers = random.choice(headers_list)
    with requests.Session() as s:
        res = s.get(url, headers=headers)
        if not res.cookies:
            print("Error getting cookies")
            raise SystemExit(1)

但这并不能解决问题,我仍然有时在我的响应和检测中没有得到cookie。
我是这样搜集数据的:

post = s.post(url, data=login_data, headers=headers, cookies=cookies, allow_redirects=True)
        soup = BeautifulSoup(post.text, 'html.parser')
        if soup.find('input', {'name': 'appActionToken'})['value'] is not None \
                and soup.find('input', {'name': 'appAction'})['value'] is not None \
                and soup.find('input', {'name': 'subPageType'})['value'] is not None \
                and soup.find('input', {'name': 'openid.return_to'})['value'] is not None \
                and soup.find('input', {'name': 'prevRID'})['value'] is not None \
                and soup.find('input', {'name': 'workflowState'})['value'] is not None \
                and soup.find('input', {'name': 'email'})['value'] is not None:
            print("found")
        else:
            print("not found")
            raise SystemExit(1)

但是当反机器人检测到我时,这个内容将不可用,从而抛出一个错误。我如何能防止这种情况?谢谢!

20jt8wwn

20jt8wwn1#

你可以在每次Scrape操作之前设置一段时间的time.sleep(10),亚马逊会更难抓住你,但如果你发送太多的常规请求,他们也可能会检测并阻止它们。

jw5wzhpr

jw5wzhpr2#

  • 使用随机用户代理轮换请求头(使用更多用户代理更新头列表)
  • 从产品URL中删除**/dp/ASIN/**之后的所有内容(跟踪参数

例如,删除跟踪参数后,您的url将如下所示:https://www.amazon.com/Storage-Stackable-Organizer-Foldable-Containers/dp/B097PVKRYM/

  • 在请求之间添加少量睡眠(使用time.sleep()
  • 在您的请求中使用代理(您可以使用Tor代理,如果他们阻止Tor使用其他付费代理服务)

相关问题