Python 3.x中使用urllib出现AttributeError: module 'urllib' has no attribute 'request'错误

刚刚开始学习爬虫,开始写博客打算把错误记录下来,已杜自己忘记,并给同样的小白帮助

python 3.x中urllib库和urilib2库合并成了urllib库,python3.X中应该使用urllib.request,即替换掉(python中的)urllib2成urllib.request


报错信息:

Traceback (most recent call last):
  File "C:/Users/admin/Desktop/uuu.py", line 4, in <module>
    response1=urllib.request.urlopen(url)

AttributeError: module 'urllib' has no attribute 'request'


代码更改:

import urllib.request

url="http://www.baidu.com"
response1=urllib.request.urlopen(url)

print (response1.getcode())

猜你喜欢

转载自blog.csdn.net/sinat_36789271/article/details/79426752