Fix Python – How can I read the contents of an URL with Python?


Asked By – Helen Neely

The following works when I paste it on the browser:

But when I try reading the URL with Python nothing happens:

 link = ''
 f = urllib.urlopen(link)           
 myfile = f.readline()  
 print myfile

Do I need to encode the URL, or is there something I’m not seeing?

Now we will see solution for issue: How can I read the contents of an URL with Python?


To answer your question:

import urllib

link = ""
f = urllib.urlopen(link)
myfile =

You need to read(), not readline()

EDIT (2018-06-25): Since Python 3, the legacy urllib.urlopen() was replaced by urllib.request.urlopen() (see notes from for details).

If you’re using Python 3, see answers by Martin Thoma or i.n.n.m within this question: (Python 2/3 compat) (Python 3)

Or, just get this library here: and seriously use it 🙂

import requests

link = ""
f = requests.get(link)

This question is answered By – woozyking

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0