gpt4 book ai didi

python - Scrapy shell 获取 301 重定向到没有参数的 URL

转载 作者:太空狗 更新时间:2023-10-30 00:02:52 25 4
gpt4 key购买 nike

Scrapy可以使用 GET 参数请求 URL 以交互方式探索响应:

scrapy shell "https://duckduckgo.com/?q=foo"

但是对于某些网站,我的请求被 301 重定向并且 URL 参数被删除:

DEBUG: Redirecting (301) to <GET http://foo.com/mypage/> 
from <GET http://foo.com/mypage/?bar=baz>
DEBUG: Crawled (200) <GET http://foo.com/mypage/> (referer: None)

当我在浏览器中正常访问 http://foo.com/mypage/?bar=baz 时,我没有被重定向并且 GET 参数保留.

谁能建议我如何避免被重定向?

最佳答案

受到@paultrmbrth 在评论中的回答的启发,下面是使用 User Agent spoofing 解决此问题的确切方法。 .

首先,找到您浏览器的用户代理字符串(我使用 http://www.whatsmyuseragent.com/ 完成此操作,但可能还有其他方法)。

我的是

Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0

现在在 project_name/items.py 中添加以下行:

USER_AGENT = "whatever the user agent string was"

scrapy shell "http://foo.com/mypage/?bar=baz" 将按预期工作。

关于python - Scrapy shell 获取 301 重定向到没有参数的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24119840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com