python下载数据(urllib、urllib2)

发布于 2024-11-17 04:47:16 字数 549 浏览 3 评论 0原文

我有一个类似 这个,直接指向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

决绝 2024-11-24 04:47:16

urllib2 透明地处理重定向。您可能想了解服务器在呈现此类重定向并允许您下载时实际在做什么。您可能想要子类重定向处理程序并查看标头的哪个属性为您提供了url 并使用 urlretrieve 来下载它。

设置cookie,显式尝试可能也是一个不错的尝试。

import cookielib, urllib2
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.open('yourmp3filelink')

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.

import cookielib, urllib2
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.open('yourmp3filelink')
幻梦 2024-11-24 04:47:16

您的链接重定向到 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文