如何使用Python下载文件 [英] How to download a file using Python

查看:136
本文介绍了如何使用Python下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Python从互联网上下载某些东西,我使用urllib模块中的 urllib.retriever ,但是我无法使其工作。我想要将下载的文件保存到我选择的位置。
如果有人可以向我解释如何使用明确的例子,那将是非常感谢。

I tried to download something from the Internet using Python, I am using urllib.retriever from the urllib module but I just can't get it work. I would like to be able to save the downloaded file to a location of my choice. If someone could explain to me how to do it with clear examples, that would be VERY appreciated.

推荐答案

我建议使用 urllib2 ,如下所示:

I suggest using urllib2 like so:

source = urllib2.urlopen("http://someUrl.com/somePage.html").read()
open("/path/to/someFile", "wb").write(source)

你甚至可以缩短它(尽管你不想要如果您打算将每个个人电话包含在中尝试 - 除外)缩短:

You could even shorten it to (although, you wouldnt want to shorten it if you plan to enclose each individual call in a try - except):

open("/path/to/someFile", "wb").write(urllib2.urlopen("http://someUrl.com/somePage.html").read())

这篇关于如何使用Python下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆