urllib2在python3后已经合并在urllib中,
具体为urllib.response,urllib.request
所以下面代码在3.x上会报错
具体解决办法:
import urllib
import urllib.response
import urllib.request
import urllib.parse
import http.cookiejar
url = "http://www.baidu.com"
print("第一种方法")
response1 = urllib.request.urlopen(url)
print(response1.getcode())
print(len(response1.read()))
print("第二种方法")
request = urllib.request.Request(url)
request.add_header("user-agent", "Mozilla/5.0")
response2 = urllib.request.urlopen(request)
print(response2.getcode())
print(response2.read)
#print(len(response2.read))
具体的urllib2用urllib.request或urllib.response代替目前还是不清楚的
自己试试就知道了!
路漫漫兮修远兮,学吧!