繁体   English   中英

亚马逊抓取 - 抓取有时有效

[英]Amazon Scraping - Scraping works sometimes

我出于教育目的从亚马逊抓取数据,我在使用 cookies 和 antibot 时遇到了一些问题。 我设法抓取了数据,但有时 cookies 不会出现在响应中,或者反机器人标记了我。

我已经尝试使用这样的随机标题列表:

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)

但是当 antibot 检测到我时,这个内容将不可用,从而抛出错误。 关于如何防止这种情况的任何想法? 谢谢!

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

  • 使用随机用户代理轮换您的请求标头(使用更多用户代理更新您的标头列表)

  • 从产品 url 中删除/dp/ASIN/之后的所有内容(跟踪参数)

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

  • 在请求之间添加小睡眠(使用time.sleep()

  • 对您的请求使用代理(您可以使用 Tor 代理,如果它们使用其他付费代理服务阻止 Tor go)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM