詳解python3 urllib中urlopen報錯的解決方法

高洛峰
發布: 2017-03-28 09:44:59
原創
2681 人瀏覽過

這篇文章主要介紹了關於解決python3 urllib中urlopen報錯問題的相關資料,文中介紹的非常詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。

#最近更新了Python版本,準備寫個爬蟲,意外的發現urllib庫中屬性不存在urlopen,於是各種google,然後總結一下給出解決方案

問題的出現​​

AttributeError: 'module

'

object详解python3 urllib中urlopen报错的解决方法 has no attribute 'urlopen '

問題的解決途徑

#我們先來看下官方文件的解釋: #

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中,也就是整合了。

#
import urllib.request 
url="http://www.baidu.com" 
get=urllib.request.urlopen(url).read() 
print(get)
登入後複製

結果示意圖:

其實也是可以換個utf-8的編碼讓讀取出來的原始碼更正確的,但這已經是番外的不再提了。

以上是詳解python3 urllib中urlopen報錯的解決方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!