python3中urlopen_解决python3 urllib中urlopen报错的问题
前言
最近更新了Python版本,準(zhǔn)備寫個(gè)爬蟲,意外的發(fā)現(xiàn)urllib庫(kù)中屬性不存在urlopen,于是各種google,然后總結(jié)一下給出解決方案
問(wèn)題的出現(xiàn)
AttributeError: 'module' object has no attribute 'urlopen'
問(wèn)題的解決途徑
我們先來(lái)看下官方文檔的解釋:
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.)
也就是說(shuō)官方3.0版本已經(jīng)把urllib2,urlparse等五個(gè)模塊都并入了urllib中,也就是整合了。
正確的使用方法
import urllib.request
url="http://www.baidu.com"
get=urllib.request.urlopen(url).read()
print(get)
結(jié)果示意圖:
其實(shí)也是可以換個(gè)utf-8的編碼讓讀取出來(lái)的源碼更正確的,但這已經(jīng)是番外的不再提了。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
總結(jié)
以上是生活随笔為你收集整理的python3中urlopen_解决python3 urllib中urlopen报错的问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 攻城掠地端mysql_【图片】攻城掠地单
- 下一篇: python监听剪贴板_Python监听