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.

Answer from Martijn Pieters on Stack Overflow
🌐
Python
docs.python.org › 3 › library › urllib.request.html
urllib.request — Extensible library for opening URLs — Python ...
Note that params output from urlencode ... 2, 'bacon': 0}) >>> data = data.encode('ascii') >>> with urllib.request.urlopen("http://requestb.in/xrbl82xr", data) as f: ......
🌐
Real Python
realpython.com › urllib-request
Python's urllib.request for HTTP Requests – Real Python
January 11, 2025 - To decode the bytes with Python, all you need to find out is the character encoding used. Encoding, especially when referring to character encoding, is often referred to as a character set. As mentioned, ninety-eight percent of the time, you’ll probably be safe defaulting to UTF-8: ... >>> from urllib.request import urlopen >>> with urlopen("https://www.example.com") as response: ...
🌐
Python documentation
docs.python.org › 3 › howto › urllib2.html
HOWTO Fetch Internet Resources Using The urllib Package — Python 3.14.3 documentation
This is capable of fetching URLs using a variety of different protocols. It also offers a slightly more complex interface for handling common situations - like basic authentication, cookies, proxies and so on.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-urllib-module
Python Urllib Module - GeeksforGeeks
July 12, 2025 - Urllib package is the URL handling module for python. It is used to fetch URLs (Uniform Resource Locators). It uses the · urlopen · function and is able to fetch URLs using a variety of different protocols.
🌐
Python
docs.python.org › 3 › library › urllib.html
urllib — URL handling modules
Source code: Lib/urllib/ urllib is a package that collects several modules for working with URLs: urllib.request for opening and reading URLs, urllib.error containing the exceptions raised by urlli...
🌐
Real Python
realpython.com › ref › stdlib › urllib
urllib | Python Standard Library – Real Python
Python · >>> import urllib.request >>> response = urllib.request.urlopen("http://www.example.com") >>> html = response.read() >>> html[:60] b'<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>' Opens and reads URLs · Parses and constructs URLs ·
🌐
Simplilearn
simplilearn.com › home › resources › software development › python internet access using urllib.request and urlopen()
Python Internet Access Using Urllib.Request and Urlopen() | Simplilearn
October 9, 2024 - This article explains the Urllib python, urllib request urlopen() functions present in Python, which help in accessing the Internet using Python. Explore now.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
Find elsewhere
🌐
ArjanCodes
arjancodes.com › blog › using-python-urllib-module-for-web-scraping-and-url-parsing
Web Scraping and URL Parsing Made Easy with Python’s urllib | ArjanCodes
July 29, 2024 - The urllib module is a powerful tool for working with URLs in Python. Whether you’re fetching data from the web, parsing URLs, handling encodings, or managing cookies, urllib has you covered.
🌐
Python Programming
pythonprogramming.net › urllib-tutorial-python-3
Python urllib tutorial for Accessing the Internet
If you are using the default python user-agent with urllib, then you are announcing yourself as Python-urllib/3.4, if your Python version is 3.4. This is either foreign to the website, or they will just block it entirely. A work around for this is to just identify yourself as something else entirely. try: x = urllib.request.urlopen('https://www.google.com/search?q=test') #print(x.read()) saveFile = open('noheaders.txt','w') saveFile.write(str(x.read())) saveFile.close() except Exception as e: print(str(e)) The above output is from Google, who knows you are Python.
🌐
ProxiesAPI
proxiesapi.com › articles › accessing-websites-in-python-with-urllib-request-urlopen
Accessing Websites in Python with urllib.request.urlopen | ProxiesAPI
The urllib.request module in Python 3 provides a simple way to access and download data from websites via HTTP and HTTPS. The key function for this is urllib.request.urlopen(), which opens a handle to a URL that you can then read from or write to.
🌐
Read the Docs
stackless.readthedocs.io › en › 2.7-slp › library › urllib.html
20.5. urllib — Open arbitrary resources by URL — Stackless-Python 2.7.15 documentation
In particular, the urlopen() function is similar to the built-in function open(), but accepts Universal Resource Locators (URLs) instead of filenames. Some restrictions apply — it can only open URLs for reading, and no seek operations are available. ... The Requests package is recommended ...
🌐
Python Module of the Week
pymotw.com › 2 › urllib2
urllib2 – Library for opening URLs. - Python Module of the Week
import urllib import urllib2 query_args = { 'q':'query string', 'foo':'bar' } encoded_args = urllib.urlencode(query_args) print 'Encoded:', encoded_args url = 'http://localhost:8080/?' + encoded_args print urllib2.urlopen(url).read() The list of client values returned in the example output contains the encoded query arguments. $ python urllib2_http_get_args.py Encoded: q=query+string&foo=bar CLIENT VALUES: client_address=('127.0.0.1', 55849) (localhost) command=GET path=/?q=query+string&foo=bar real path=/ query=q=query+string&foo=bar request_version=HTTP/1.1 SERVER VALUES: server_version=BaseHTTP/0.3 sys_version=Python/2.6.2 protocol_version=HTTP/1.0 HEADERS RECEIVED: accept-encoding=identity connection=close host=localhost:8080 user-agent=Python-urllib/2.6
🌐
Guru99
guru99.com › home › python › python internet access using urllib.request and urlopen()
Python Internet Access using Urllib.Request and urlopen()
August 12, 2024 - # # read the data from the URL and print it # import urllib.request # open a connection to a URL using urllib webUrl = urllib.request.urlopen('https://www.youtube.com/user/guru99com') #get the result code and print it print ("result code: " + str(webUrl.getcode())) # read the data from the URL and print it data = webUrl.read() print (data)
🌐
Python Module of the Week
pymotw.com › 2 › urllib
urllib – simple interface for network resource access - Python Module of the Week
An HTTP GET operation is the simplest use of urllib. Simply pass the URL to urlopen() to get a “file-like” handle to the remote data.
🌐
Educative
educative.io › answers › what-is-the-urllib-module-in-python-3
What is the urllib module in Python 3?
Urllib is a Python 3 package that ... are shown in the illustration below: Using urllib.request, with urlopen, allows you to open the specified URL....
🌐
Medium
medium.com › nishkoder › working-with-pythons-urllib-library-a-beginner-s-guide-79bcad776a8e
Working with Python’s urllib Library: A Beginner’s Guide | by Nishant Gupta | Medium
March 26, 2023 - Before you can use urllib.request, you need to import it into your Python script: ... The urlopen function returns an HTTPResponse object containing the server's response.
🌐
GitHub
github.com › python › cpython › blob › main › Lib › urllib › request.py
cpython/Lib/urllib/request.py at main · python/cpython
f = urllib.request.urlopen('https://www.python.org/') """ · # XXX issues: # If an authentication error handler that tries to perform · # authentication for some reason but fails, how should the error be · # signalled? The client needs to know the HTTP error code.
Author   python