如何使用python请求获取重定向url [英] how to get redirect url using python requests

查看:40
本文介绍了如何使用python请求获取重定向url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

r = requests.get('http://techtv.mit.edu/videos/1585-music-session-02/download.source') 
for i in r.history:
    print(i.url) 

我认为它应该打印出历史记录,但它没有,上面的网址指向一个视频,但我无法得到它,有人帮忙吗?谢谢

I think it should print out the history, but it doesn't, the above url points to a video, but I cannot get it, anyone help? Thank you

推荐答案

要在重定向后获取结果 URL,可以执行 r.url.

To get the resultant URL after you've been redirected, you can do r.url.

r = requests.get('https://youtu.be/dQw4w9WgXcQ') 
print(r.url) # https://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtu.be

r.history 用于最后一个 URL 之前的 URL,因此它只会返回您的原始 URL,因为您只被重定向了一次.

r.history is for URLs prior to the final one, so it's only returning your original URL because you were only redirected once.

这篇关于如何使用python请求获取重定向url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆