00x0 前言
最近更新了python版本,準備寫個爬蟲,意外的發現urllib庫中屬性不存在urlopen,於是各種google,然後總結一下給出解決方案
00x1 問題的出現
AttributeError: 'module' object has no attribute 'urlopen'
00x2 問題的解決途徑
我們先來看下官方文檔的解釋:
a new urllib package was created. It consists of code from
urllib, urllib2, urlparse, and robotparser. The old
modules have all been removed. The new package has five submodules:
urllib.parse, urllib.request, urllib.response,
urllib.error, and urllib.robotparser. The
urllib.request.urlopen() function uses the url opener from
urllib2. (Note that the unittests have not been renamed for the
beta, but they will be renamed in the future.)
也就是說官方3.0版本已經把urllib2,urlparse等五個模塊都並入了urllib中,也就是整合了。
00x3 正確的使用方法
import urllib.request
url="http://www.baidu.com"
get=urllib.request.urlopen(url).read()
print(get)
結果示意圖:
其實也是可以換個utf-8的編碼讓讀取出來的源碼更正確的,但這已經是番外的不再提了。