如何抓取“这个” url 使用 urllib 吗?
我尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你使用什么操作系统?这行代码:
在 Windows 上打开一个文本模式文件,这将执行您不想要的操作。将其更改为
(以二进制模式打开文件)是否可以正常工作?
What OS are you on? This line of code:
opens a text-mode file on Windows, which will do things that you don't want. Does changing that to
(opening the file in binary mode) make things work correctly?
您在代码中使用的 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. :)