You are reading the wrong documentation or the wrong Python interpreter version. You tried to use the Python 3 library in Python 2.
Use:
import urllib2
sock = urllib2.urlopen("http://diveintopython.org/")
htmlSource = sock.read()
sock.close()
print htmlSource
The Python 2 urllib2 library was replaced by urllib.request in Python 3.
You are reading the wrong documentation or the wrong Python interpreter version. You tried to use the Python 3 library in Python 2.
Use:
import urllib2
sock = urllib2.urlopen("http://diveintopython.org/")
htmlSource = sock.read()
sock.close()
print htmlSource
The Python 2 urllib2 library was replaced by urllib.request in Python 3.
In Python3 you can use urllib or urllib3
urllib:
import urllib.request
with urllib.request.urlopen('http://docs.python.org') as response:
htmlSource = response.read()
urllib3:
import urllib3
http = urllib3.PoolManager()
r = http.request('GET', 'http://docs.python.org')
htmlSource = r.data
More details could be found in the urllib or python documentation.
Videos
This happens in the console.
I checked to see if Requests is installed, and it is.
pip install requests
dependency already satisfied.
Anyways, this appears to be one of the better Beautifulsoup tutorials on the web, and I'd prefer to use the console rather than just the script file each time while learning.