1

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.

1

2 Answers 2

7

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())
Sign up to request clarification or add additional context in comments.

3 Comments

You might consider using "wb" rather than "w" in case the downloaded file is binary.
Thanks it working very well, now I just have to understand why exactly. =)
@user which part doesnt make sense? Ill try to explain more if possible.
1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.