Skip to content Skip to sidebar Skip to footer

Urllib.request For Python 3.3 Not Working To Download File

I would like to download a large archive file with python and save it, but urllib is not working for me. This is my code: import urllib urllib.request('http://www.petercoll

Solution 1:

No, it is not broken. The urllib.request documentation is pretty clear on how this works:

import urllib.requestreq= urllib.request.urlopen('http://www.google.com')
data = req.read()

Edit: If you need to write the file directly to disk rather than process the data, use urlretrieve.

urllib.request.urlretrieve('http://example.com/big.zip', 'file/on/disk.zip')

Solution 2:

To download an url into a file, you could use urlretrieve() function:

from urllib.request importurlretrieveurl="http://www.petercollingridge.co.uk/sites/files/peter/particle_tutorial_7.txt"
urlretrieve(url, "result.txt")

Solution 3:

The urllib2 module has been split across several modules in Python 3.0 named urllib.request and urllib.error. The 2to3 tool will automatically adapt imports when converting your sources to 3

from urllib.request import urlopen

data = urlopen(r"http://www.petercollingridge.co.uk/sites/files/peter/particle_tutorial_7.txt")

print(data)

Post a Comment for "Urllib.request For Python 3.3 Not Working To Download File"