[source code] Python Programming Tutorial - 22 - Download an Image from the Web

+5 Bucky Roberts · September 1, 2014

import random
import urllib.request

def download_web_image(url):
    name = random.randrange(1, 1000)
    full_name = str(name) + ".jpg"
    urllib.request.urlretrieve(url, full_name)


Post a Reply


- page 1
Oldest  Newest  Rating
-1 Gary Murphy · September 10, 2014
Hey Bucky,

I went to install urllib.request but it  doesn't appear in the list of available packages.  I use Python 2.7 at the moment because of a course I'm doing at the moment and someone mentioned in the Youtube comments to use the urllib package, but that one doesn't appear either.  There's a whole bunch of other urllib packages (eg. urllib2, urllib3, urllibcache, etc..), but I don't know whether they'll have what I need.  Any ideas?
0 Gary Murphy · September 10, 2014
Turns out urllib is a basic module in Python 2.7 and doesn't have to be installed.
Works well.  ;)
0 Yoncho Yonchev · September 11, 2014

I am using requests instead and it works like a charm.  Using shutil, that is standard library to work with directories.

Something like


if r.status_code is 200:
with open(directory+filename,'wb') as f:
-1 Jakob Jensen · September 28, 2014
Hi Bucky and fellow Bucky followers

My PyCharm can't find the module urllib.request :dizzy:

Traceback (most recent call last):
  File "/Users/jakobjensen/PycharmProjects/PythonSchool/Files.py", line 3, in <module>
    import urllib.request
ImportError: No module named request

I have no idea why it can't find it :'( I got the idea that it was a standard module in PyCharm.

- Jakob Jensen
0 Nathanael Potter · September 28, 2014
Post your code Jakob and we can have a look! :)
0 Jakob Jensen · September 28, 2014
import random
import urllib.request

def Download_web_image(url):
    name = random.randrange(1, 1000)
    full_name = str(name) + ".jpg"
    urllib.request.urlretrieve(url, full_name)


+1 Jakob Jensen · October 1, 2014
I found the solution to my problem :D

my project interpreter was by default set to python 2.7 so thats why it didn't have urllib.request by default 
0 MAK Leung · October 17, 2014
How could you solve the problem ? I have the same problem but do not know how to solve
0 Jakob Jensen · October 25, 2014

go to preferences - project interpreter - click the project interpreter drop down menu - choose version 3.4.1 or whatever version you have installed. :)

hope that solves it.

- Jakob
0 soso ninidze · December 30, 2014
hello  .

i need  tp  study 2,7  to receive  a job.  
so im trying to study by your  tutorials about  3,4.  is  there  be a problem?  

and  also im trying to study  wihtout  py charm.  and yet  i had no  problems writing it in default  idle (python gui)/
 but  trying this  project,  when i was  practicing  it showed  error i couldnt  understand  why. so can problem be  that u are using  pycharm?

thank for responce
  • 1
  • 2



This section is all about snakes! Just kidding.

Bucky Roberts Administrator