运行时报错:AttributeError: module 'urllib' has no attribute 'urlopen'

 在Python3中如果写以下代码会出现报错,原因就是在Python3中应该把            import urllib=======》import urllib.request

把所有位置都换成带有.request,就可以解决错误了

import urllib
from bs4 import BeautifulSoup

response = urllib.urlopen("http://www.imau.edu.cn")
html = response.read()
data = html.decode('utf-8')
soup = BeautifulSoup(data)
# print soup.findAll('span')

response = urllib.urlopen("http://www.imau.edu.cn")
AttributeError: module 'urllib' has no attribute 'urlopen'

例如这个例子改一下:两处改好就OK了

import urllib.request
from bs4 import BeautifulSoup

response = urllib.request.urlopen("http://www.imau.edu.cn")
html = response.read()
data = html.decode('utf-8')
soup = BeautifulSoup(data)
# print soup.findAll('span')

撒花撒花~~

猜你喜欢

转载自blog.csdn.net/qq_41233643/article/details/87350100