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

+4 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 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 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 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)


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:
0 James ciak · January 13, 2015
After coping the code directly. It works fine in Pycharm. I feel the error is on our part.
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
0 MAK Leung · October 17, 2014
How could you solve the problem ? I have the same problem but do not know how to solve
-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
  • 1
  • 2



This section is all about snakes! Just kidding.

Bucky Roberts Administrator