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
Note that params output from urlencode is encoded to bytes before it is sent to urlopen as data: >>> import urllib.request >>> import urllib.parse >>> data = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) >>> data = data.encode('ascii') >>> with urllib.request.urlopen("http://requestb.in/xrbl82xr", data) as f: ...
๐ŸŒ
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.
๐ŸŒ
Real Python
realpython.com โ€บ urllib-request
Python's urllib.request for HTTP Requests โ€“ Real Python
January 11, 2025 - In this example, you import urlopen() from the urllib.request module. You use the with keyword with .urlopen() to assign the HTTPResponse object to the variable response. Then, you read the first fifty bytes of the response and then read the following fifty bytes, all within the with block.
๐ŸŒ
Atlassian
jira.atlassian.com โ€บ browse โ€บ BCLOUD-1280
Python urllib.urlopen() doesn't work with one of your URLs
site = 'http://bitbucket.org/tortoisehg/stable/wiki/Home/ReleaseNotes'\\ req = urllib.urlopen(site) text = req.read()
๐ŸŒ
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 ...
Find elsewhere
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ howto โ€บ urllib2.html
HOWTO Fetch Internet Resources Using The urllib Package โ€” Python 3.14.4 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.
๐ŸŒ
Narkive
comp.lang.python.narkive.com โ€บ 4T3mcYGC โ€บ python-2-3-urllib-urlopen
Python 2 -> 3, urllib.urlOpen
My question is: Is there a simple (hopefully one or two line) replacement for my call to url lib.urlopen(<URL>).read() I know that there are other modules out there that handle requests (like the Requests module), but this is a strictly controlled university environment. I cannot download any external packages (other then pygame, which I got special permission for) onto the computers in the school. Therefore, I'm looking for something in the Python 3.6 Standard Library. Thanks in advance, Irv ... Post by Irv Kalb import urllib # set the Yahoo finance url, set stock name, ask for last price ful
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ urllib urlopen with list
r/Python on Reddit: Urllib urlopen with list
November 21, 2017 - I tried poking around to find how to pass a list through urllib. I can get it to work for one url but am having trouble for multiple. I wanted to see if could get it to work generally if put a counter in here and it does sort of in that it prints 0 then 0+1. Ideally this would pull from a text file but I can sort that out later.
๐ŸŒ
OMZ Software
omz-software.com โ€บ editorial โ€บ docs โ€บ library โ€บ urllib.html
20.5. urllib โ€” Open arbitrary resources by URL โ€” Editorial Documentation
The public functions urlopen() and urlretrieve() create an instance of the FancyURLopener class and use it to perform their requested actions. To override this functionality, programmers can create a subclass of URLopener or FancyURLopener, then assign an instance of that class to the urllib._ur...
๐ŸŒ
ProxiesAPI
proxiesapi.com โ€บ articles โ€บ retrieving-and-parsing-text-from-urls-with-python-s-urllib
Retrieving and Parsing Text from URLs with Python's urllib | ProxiesAPI
The urllib module in Python provides tools for retrieving and parsing content from URLs. It can fetch text content, parse HTML and JSON, and handle errors.
๐ŸŒ
Lsu
ld2016.scusa.lsu.edu โ€บ python-2.7.8-docs-html โ€บ library โ€บ urllib.html
20.5. urllib โ€” Open arbitrary resources by URL โ€” Python 2.7.8 documentation
September 20, 2014 - The public functions urlopen() and urlretrieve() create an instance of the FancyURLopener class and use it to perform their requested actions. To override this functionality, programmers can create a subclass of URLopener or FancyURLopener, then assign an instance of that class to the urllib._ur...
๐ŸŒ
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
๐ŸŒ
Medium
aignishant.medium.com โ€บ 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 - import urllib.request import urllib.error url = 'https://example.com' try: response = urllib.request.urlopen(url) content = response.read() content = content.decode('utf-8') print(content) except urllib.error.HTTPError as eโ€ฆ
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ >>> from urllib.request import urlopen importerror: no module named request
r/learnpython on Reddit: >>> from urllib.request import urlopen ImportError: No module named request
December 23, 2014 -

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.

๐ŸŒ
DEV Community
dev.to โ€บ bowmanjd โ€บ hardening-and-simplifying-python-s-urlopen-4gee
Hardening and Simplifying Python's urlopen - DEV Community
February 9, 2026 - While researching and writing, I learned of the OpenerDirector class. This class offers the opportunity to streamline urlopen(), make it more secure, and provide custom error handling. ... from urllib.request import urlopen url = "file:///etc/passwd" with urlopen(url) as response: print(response.read().decode())
๐ŸŒ
Python Module of the Week
pymotw.com โ€บ 2 โ€บ urllib2
urllib2 โ€“ Library for opening URLs. - Python Module of the Week
July 19, 2009 - The return value from urlopen() gives access to the headers from the HTTP server through the info() method, and the data for the remote resource via methods like read() and readlines(). $ python urllib2_urlopen.py RESPONSE: <addinfourl at 11940488 whose fp = <socket._fileobject object at 0xb573f0>> URL : http://localhost:8080/ DATE : Sun, 19 Jul 2009 14:01:31 GMT HEADERS : --------- Server: BaseHTTP/0.3 Python/2.6.2 Date: Sun, 19 Jul 2009 14:01:31 GMT LENGTH : 349 DATA : --------- CLIENT VALUES: client_address=('127.0.0.1', 55836) (localhost) command=GET path=/ real path=/ query= 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
๐ŸŒ
Code
ggi.code.blog โ€บ urllib-request-and-urlopen
Urllib.Request and urlopen()
November 30, 2022 - # # read the data from the URL and print it # import urllib2 def main(): # open a connection to a URL using urllib2 webUrl = urllib2.urlopen("https://www.youtube.com/user/ggicom") #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 if __name__ == "__main__": main()
๐ŸŒ
CyuBlog
cyublog.com โ€บ articles โ€บ python-en โ€บ urllib-request-sample
Python: Use urllib.request to do http request
May 18, 2021 - In this article, I will show you how to execute HTTP requests with urllib.request. The workflow is, 1. Create a request by `urllib.request.Request` method. 2. Assign the request as a parameter of `urllib.request.urlopen` method to get response. 3. use `json` module to parse the response.
๐ŸŒ
Real Python
realpython.com โ€บ ref โ€บ stdlib โ€บ urllib
urllib | Python Standard Library โ€“ Real 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 ...