python - 如何使用 Python 发出 URL 请求并返回重定向到的 URL?

标签 python python-3.x python-requests

我正在尝试访问一个 URL,它将在重定向的 URL 中返回一个代码字符串。

示例:https://exampleurl.com

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/

相关文章:

python - DELIMITER/在 SQLAlchemy 中创建触发器

python - Python3 中的语法错误

django - 如何使用 django 服务 HTTP/2 协议(protocol)

python - 出现错误 "Maximum recursion depth exceeded while calling a Python object"

python - 在 CentOS 中 Python 请求中的 SSL 错误

python - 网络抓取模拟器返回的特定值

python - 如何修改这个斐波那契数列问题?

python - 如何将训练好的模型导入keras?

python - 如何避免在每次导入后重新启动 python shell

python - 如何将多处理与请求模块一起使用?