我正在尝试访问一个 URL,它将在重定向的 URL 中返回一个代码字符串。
2-3 秒后我被重定向到 https://newurl.com/stuffhere?code=97412098512yidfh480b14r
有没有办法返回新的 URL?
最佳答案
来自official docs :
The
Response.history
list contains the Response objects that were created in order to complete the request. The list is sorted from the oldest to the most recent response.
这是一个打印指向最终 URL 的 URL 的示例:
import requests
response = requests.get('http://httpbin.org/redirect/3')
for resp in response.history:
print(resp.url)
要仅获取最终 URL,您只需执行以下操作:
print(response.url)
关于python - 如何使用 Python 发出 URL 请求并返回重定向到的 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55792005/