In Python, we often need to obtain various network resources from the Internet, such as web content, image files, etc. urllib2 is a powerful module in the Python standard library that can help us obtain and operate network resources. This article will introduce in detail how to use the urllib2 module to obtain network resources, and attach the corresponding source code.
First, we need to import the urllib2 module:
import urllib2
Next, we use the functions provided by the urllib2 module urlopen()
to open a URL and obtain its contents. Here's a simple example:
url = "http://www.example.com"
response = urllib2.urlopen(url)
html = response.read()
print