How to download a file using Python

2019-01-15 17:44发布

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.

2条回答
迷人小祖宗
2楼-- · 2019-01-15 18:10

You can also use the urllib:

source = urllib.request.urlopen(("full_url")).read()

and then use what chown used above:

open("/path/to/someFile", "wb").write(source)
查看更多
爷、活的狠高调
3楼-- · 2019-01-15 18:18

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())
查看更多
登录 后发表回答