urllib2 is used in python 2.x, so if you use urllib2 in python 3.x, you will get this error: No module named ‘urllib2’. To fix this error, we should use python 2.x or replace urllib.request to replace it.
urllib library in python 3.x contains:
- urllib.request for opening and reading URLs
- urllib.error containing the exceptions raised by urllib.request
- urllib.parse for parsing URLs
- urllib.robotparser for parsing robots.txt files
Here is an example for using urllib.request.
import urllib.request with urllib.request.urlopen('http://www.python.org/') as f: print(f.read(300))