【问题标题】:How come python requests show different result then scrapy request?为什么 python 请求显示与 scrapy 请求不同的结果?
【发布时间】:2023-01-17 05:29:03
【问题描述】:

我试图用 scrapy 抓取以下 url,但它返回 301 状态。虽然简单的 python requests 工作得很好。 网址 = https://www.depop.com/products/saltybrasi-perte-dego-white-lace-shirt/ 我在具有默认设置的任何项目之外的Scrapy Shell 中运行测试。

>>> import requests
>>> r = requests.get('https://www.depop.com/products/saltybrasi-perte-dego-white-lace-shirt/')
2023-01-16 17:01:16 [urllib3.connectionpool] DEBUG: Starting new HTTPS connection (1): www.depop.com:443
2023-01-16 17:01:17 [urllib3.connectionpool] DEBUG: https://www.depop.com:443 "GET /products/saltybrasi-perte-dego-white-lace-shirt/ HTTP/1.1" 200 None
>>> fetch('https://www.depop.com/products/saltybrasi-perte-dego-white-lace-shirt/')
2023-01-16 17:01:31 [scrapy.core.engine] INFO: Spider opened
2023-01-16 17:01:31 [scrapy.core.engine] DEBUG: Crawled (301) <GET https://www.depop.com/products/saltybrasi-perte-dego-white-lace-shirt/> (referer: None)
>>>

两者之间有什么区别,我如何使它与 Scrapy 一起工作? 谢谢。

【问题讨论】:

  • 可能是因为 scrapy 的默认用户代理。

标签: python-3.x python-requests scrapy


【解决方案1】:

网站很可能使用 cookie 来跟踪用户的会话,这是 很容易被强大的requests库克服,但不是超快的scrapy。在 scrapy 中,它阻止响应状态 200 并重定向到 301,但请求可以绕过这种阻止。 Scrapy 也可以处理,但需要代理的帮助。

例如,scrapy 无法自行处理简单的 cloudflare 保护,但请求库可以顺利处理。这就是为什么它在请求中工作(200),而不是在 scrapy 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-25
    • 2018-05-28
    • 1970-01-01
    相关资源
    最近更新 更多