如何使用Python下载文件

use*_*824 1 python download

我尝试使用Python从Internet下载一些东西,我使用urllib.retriever的是urllib模块,但我无法让它工作.我希望能够将下载的文件保存到我选择的位置.如果有人能够通过明确的例子向我解释如何做到这一点,那将非常感激.

cho*_*own 7

我建议像这样使用urllib2:

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

你甚至可以缩短它(但是,如果你打算将每个单独的电话括在一个try- 中,你不想缩短它except):

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