如何抓取“这个” url 使用 urllib 吗?

发布于 2024-12-11 21:51:59 字数 537 浏览 4 评论 0原文

我尝试使用urllib爬取这个文件: http://www.anzhi.com/dl_app .php?s=68611,但总是下载错误的文件(尺寸较小)。但是,如果我在 Chrome 上打开此链接,则一切顺利并且下载的文件大小正确。代码已附上,请问有什么问题吗?

import urllib

apk = "http://sc.hiapk.com/Download.aspx?aid=294091"
local=r'x.apk'

webFile = urllib.urlopen(apk)

localFile = open(local, "w")
realurl = webFile.geturl()
print realurl
realFile = urllib.urlopen(realurl)
localFile.write(realFile.read())
webFile.close()
realFile.close()
localFile.close()

I try to use urllib to crawl this file: http://www.anzhi.com/dl_app.php?s=68611, but always download a wrong file(size smaller). However, if I open up this link on chrome, it goes well and the downloaded file size is correct. The code is attached, what's the problem?

import urllib

apk = "http://sc.hiapk.com/Download.aspx?aid=294091"
local=r'x.apk'

webFile = urllib.urlopen(apk)

localFile = open(local, "w")
realurl = webFile.geturl()
print realurl
realFile = urllib.urlopen(realurl)
localFile.write(realFile.read())
webFile.close()
realFile.close()
localFile.close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

假情假意假温柔 2024-12-18 21:51:59

你使用什么操作系统?这行代码:

localFile = open(local, "w")

在 Windows 上打开一个文本模式文件,这将执行您不想要的操作。将其更改为

localFile = open(local, "wb")

(以二进制模式打开文件)是否可以正常工作?

What OS are you on? This line of code:

localFile = open(local, "w")

opens a text-mode file on Windows, which will do things that you don't want. Does changing that to

localFile = open(local, "wb")

(opening the file in binary mode) make things work correctly?

莳間冲淡了誓言ζ 2024-12-18 21:51:59

您在代码中使用的 URL 与您在问题中询问的 URL 不同。使用 anzhi.com URL,您将获得所需的文件。 :)

You're not using the same URL in your code that you're asking about in the question. Use the anzhi.com URL and you'll get the file you want. :)

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