Andrew Andrew - 3 months ago 10x
Python Question

Get webpage contents with Python?

I'm using Python 3.1, if that helps.

Anyways, I'm trying to get the contents of this webpage. I Googled for a little bit and tried different things, but they didn't work. I'm guessing that this should be an easy task, but...I can't get it. :/.

Results of urllib, urllib2:

>>> import urllib2
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import urllib2
ImportError: No module named urllib2
>>> import urllib
>>> urllib.urlopen("")
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
AttributeError: 'module' object has no attribute 'urlopen'

Python 3 solution

Thank you, Jason. :D.

import urllib.request
page = urllib.request.urlopen('')


Because you're using Python 3.1, you need to use the new Python 3.1 APIs.



Alternately, it looks like you're working from Python 2 examples. Write it in Python 2, then use the 2to3 tool to convert it. On Windows, is in \python31\tools\scripts. Can someone else point out where to find on other platforms?


These days, I write Python 2 and 3 compatible code by using six.

from six.moves import urllib

Assuming you have six installed, that runs on both Python 2 and Python 3.