python3關於urllib中urlopen報錯問題的解決

 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的編碼讓讀取出來的源碼更正確的,但這已經是番外的不再提了。



發佈了39 篇原創文章 · 獲贊 48 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章