python下载数据(urllib、urllib2)
我有一个类似 这个,直接指向 mp3 文件。因此,当我将其放入浏览器中时,基本上会询问我是否要下载该文件,但是当我通过以下代码使用 python 执行相同的操作时:
> data = urllib2.urlopen("http://www23.zippyshare.com/d/44123087/497548/Lil%20Wayne%20ft.%20Eminem%20-%20Drop%20The%20World.mp3".read())
我将重定向到另一个像这样的链接。因此,我得到的不是 MP3 数据,而是 html 代码
'http://www23.zippyshare.com/v/44123087/file.html'
有什么想法吗? 谢谢
I have a link like this, direct to a mp3 file. So when I put it in my browser, basically asks me if I want to download the file, however when I do the same thing with python by the following code :
> data = urllib2.urlopen("http://www23.zippyshare.com/d/44123087/497548/Lil%20Wayne%20ft.%20Eminem%20-%20Drop%20The%20World.mp3".read())
I will redirected to another link like this. Therefore, instead of the MP3 data, I am getting the html code for
'http://www23.zippyshare.com/v/44123087/file.html'
any ideas ?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
urllib2 透明地处理重定向。您可能想了解服务器在呈现此类重定向并允许您下载时实际在做什么。您可能想要子类重定向处理程序并查看标头的哪个属性为您提供了url 并使用 urlretrieve 来下载它。
设置cookie,显式尝试可能也是一个不错的尝试。
urllib2 handles redirection transparently. You might want to see what the server is actually doing when it is presenting such a redirection as well allowing you to download. You might want to subclass the redirect handler and see which property of the header is giving you the url and use urlretrieve to download that.
Setting the cookies, trying explicitly might be a good try as well.
您的链接重定向到 HTML 网页,很可能是因为您的下载请求超时。这些下载网站通常是这样工作的:您永远不会获得下载的静态链接,只有临时分配的链接。
我的猜测是,无法使用该网站获取该静态链接。您必须知道该文件实际上来自哪里。
所以不,你的 python 代码没有任何问题;只是你的消息来源。
Your link redirects to an HTML webpage, most likely because your download request is timing out. That's often how these download websites work: you never get a static link to the download, only a temporarily assigned link.
My guess is that there's no way to get that static link using that website. You'd have to know where that file was actually coming from.
So no, nothing is wrong with your python code; just your sources.