1. 程式人生 > >Python 3.x中使用urllib出現AttributeError: module 'urllib' has no attribute 'request'錯誤

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())