如何获取网络上文件的真实 URL。 (Python)
我注意到有时互联网上的音频文件有一个“假”URL。
http://garagaeband.com/3252243
这将 302 到真实的 URL:
http://garageband.com/michael_jackson4.mp3
我的问题是......当提供假 URL 时,如何从标头获取真实的 URL?
目前,这是我用于读取文件头的代码。我不知道这段代码能否实现我想要的目标。如何从响应标头中解析出“真实”URL?
import httplib
conn = httplib.HTTPConnection(head)
conn.request("HEAD",tail)
res = conn.getresponse()
这有一个 302 重定向: http://www.garageband.com/mp3cat/.UZCMYiqF7Kum /01_No_pierdas_la_fuente_del_gozo.mp3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 urllib.getUrl()
编辑:
抱歉,我有一段时间没有这样做了:
例如:
Use urllib.getUrl()
edit:
Sorry, I haven't done this in a while:
For example:
Mark Pilgrim 建议在“httplib2。 org/http-web-services.html#httplib2-redirects" rel="nofollow noreferrer">深入了解 Python3" 因为它以更智能的方式处理许多事情(包括重定向)。
Mark Pilgrim advises to use httplib2 in "Dive Into Python3" as it handles many things (including redirects) in a smarter way.
您必须读取响应,意识到您收到了 302(FOUND),并从响应标头中解析出真实的 URL,然后使用新的 URI 获取资源。
You have to read the response, realize that you got a 302 (FOUND), and parse out the real URL from the response headers, then fetch the resource using the new URI.
我解决了答案。
I solved the answer.