python: No module named 'urllib2' problem and solution

If you are using a version above Python 3.x, please note that urllib and urllib2 of versions above python 3.x have been integrated into one package urllib

The code seen on the Internet, we need to change the calling method when we learn

# 用urllib.request 代替原来的 urllib2
import urllib.request
 
url = "http://www.baidu.com"
#用urllib.request.urlopen()  代替 urllib2.urlopen() 
response1 = urllib.request.urlopen(url)
#打印请求的状态码
print(response1.getcode())
#打印请求的网页内容的长度
print(len(response1.read()))

Guess you like

Origin blog.csdn.net/zhizhengguan/article/details/130427194