Nathan2055 Nathan2055 - 29 days ago 19
Python Question

How do I download a file using urllib.request in Python 3?

So, I'm messing around with

urllib.request
in Python 3 and am wondering how to write the result of getting an internet file to a file on the local machine. I tried this:

g = urllib.request.urlopen('http://media-mcw.cursecdn.com/3/3f/Beta.png')
with open('test.png', 'b+w') as f:
f.write(g)


But I got this error:

TypeError: 'HTTPResponse' does not support the buffer interface


What am I doing wrong?

NOTE: I have seen this question, but it's related to Python 2's
urllib2
which was overhauled in Python 3.

Answer Source

change

f.write(g)

to

f.write(g.read())