Python 请求:GET 返回 405 但手动使用相同的标头工作

问题描述 投票:0回答:0

我正在尝试通过以下链接调用 GET 请求:https://www.sivasdescalzo.com/en/contact/manage/preferences/hash/aefc79883b2411a6830d5ac265170e32596df603b4c14caae96d2062026a33a8e8c9ee34317d0 9f3151ebf7c692b4b7945ad48f88853d00c0ee3a550d1e3dc88?deliveryName=DM3985。 (这是来自 https://t1.news.sivasdescalzo.com/r/?id=h8e98f70,2d60df8,2d6f801&e=cDE9YWVmYzc5ODgzYjI0MTFhNjgzMGQ1YWMyNjUxNzBlMzI1OTZkZjYwM2I0YzE0Y2 的重定向 URL FhZTk2ZDIwNjIwMjZhMzNhOGU4YzllZTM0MzE3ZDA5ZjMxNTFlYmY3YzY5MmI0Yjc5NDVhZDQ4Zjg4ODUzZDAwYzBlZTNhNTUwZDFlM2RjODg&s=U6qS6rMRcR1sEYc9fIjnVzpik- _HeCeD6qxCJd7Xfug)

但是,我只收到错误 405 - 方法不允许。

我还手动打开了第二个链接,被重定向到第一个链接,并用 Proxyman (Screenshot) 检查了 https 流量。我发送的参数与我手动发送的参数相同。 我的代码片段:

link = "https://t1.news.sivasdescalzo.com/r/?id=h8e98f70,2d60df8,2d6f801&e=cDE9YWVmYzc5ODgzYjI0MTFhNjgzMGQ1YWMyNjUxNzBlMzI1OTZkZjYwM2I0YzE0Y2FhZTk2ZDIwNjIwMjZhMzNhOGU4YzllZTM0MzE3ZDA5ZjMxNTFlYmY3YzY5MmI0Yjc5NDVhZDQ4Zjg4ODUzZDAwYzBlZTNhNTUwZDFlM2RjODg&s=U6qS6rMRcR1sEYc9fIjnVzpik-_HeCeD6qxCJd7Xfug"[enter image description here](https://i.stack.imgur.com/ockpE.png)
headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "de-DE,de;q=0.9"
}

# Retrieve redirection URL and cookies 
response = requests.head(link, headers=headers)
new_url = response.headers["Location"]
cookies = "; ".join(f"{key}={response.cookies[key]}" for key in response.cookies.keys())

headers["Cookie"] = cookies


response = requests.get(new_url, headers=headers)

解决这个问题的最佳方法是什么?我将如何调试我的问题?

我试过 .head 并尝试检查允许的方法:

r = requests.options(new_url)
allow = r.headers.get("Allow", "allow not found")
print(allow)

两者都没有帮助。

如果有人能帮我解决这个问题,我将不胜感激!理想情况下,告诉我我将如何调试问题,而不仅仅是解决方案。提前致谢!

html python-requests get http-status-code-405 proxyman
© www.soinside.com 2019 - 2024. All rights reserved.