Import error no urllib2 module
这是我的代码:import urllib2.request
response = urllib2.urlopen("http://www.google.com")
html = response.read()报了如下错误
Import error no urllib2 module
如urllib2文档中所述:
在Python 3中,该urllib2模块的几个模块重命名urllib.request和urllib.error。
所以你应该使用下面代码
from urllib.request import urlopen
html = urlopen("http://www.google.com/")
print(html)
对于使用Python 2(测试版本2.7.3和2.6.8)和Python 3(3.2.3和3.3.2+)的脚本,请尝试:
#! /usr/bin/env python
try:
# For Python 3.0 and later
from urllib.request import urlopen
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen
html = urlopen("http://www.google.com/")
print(html.read())
试试这个
import urllib.request
url = "http://www.google.com/"
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
print (response.read().decode('utf-8'))
页:
[1]