python3.x中的urllib模塊

python3.x中的urllib模塊

最新版的python3.3.0已經發布了。相較於python3.0,3.2的改動並不大。但網上的大量的教程等大都以2.x版本爲基礎。這爲想要從python3.0學起的菜鳥帶來了很多的困難。
做爲一隻菜鳥,最近想學習一下python中urllib模塊的使用方法。從網上找的最簡單的實例:把google 首頁的html抓取下來並顯示在控制檯上
代碼:html

import    urllib
print    urllib.urlopen('http://www.google.com').read()

首先,使用過python3.0的朋友都知道,print已經變成含樹了,須要括號。但這不是主要問題。問題是控制檯顯示錯誤,說urllib模塊中沒有urlopen方法。
奇怪了,網上的教程能錯了?又嘗試help(urllib),發現什麼方法都沒有,只提供了package contents,裏面有5個名字。
做爲一名新手,第一感受是十分慌亂,是否是IDLE壞了,重裝試試。。。固然全部嘗試都以失敗了結。
最後,我嘗試着找找python3.3.0的change log。裏面並無找到urllib的修改記錄。然而這一修改記錄卻在python3.0的change log中。
原文是這樣寫的:python


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.)python3.x


原來,3.0版本中已經將urllib二、urlparse、和robotparser併入了urllib中,而且修改urllib模塊,其中包含5個子模塊,便是help()中看到的那五個名字。學習

爲了從此使用方便,在此將每一個包中包含的方法列舉以下:
urllib.error: ContentTooShortError; HTTPError; URLErrorui

urllib.parse: parse_qs; parse_qsl; quote; quote_from_bytes; quote_plus; unquotegoogle

                  unquote_plus; unquote_to_bytes; urldefrag; urlencode; urljoin; urlparse;
                  urlsplit; urlunparse; urlunsplit

urllib.request: AbstractBasicAuthHandler; AbstractDigestAuthHandler; BaseHandler;url

                    CatheFTPHandler; FTPHandler; FancyURLopener; FileHandler;
                    HTTPBasicAuthHandler; HTTPCookieProcessor; HTTPDefaultErrorHandler;
                    HTTPDigestAuthHandler; HTTPErrorProcessorl; HTTPHandler;
                    HTTPPasswordMgr; HTTPPasswordMgrWithDefaultRealm;
                    HTTPRedirectHandler; HTTPSHandler;OpenerDirector;ProxyBasicAuthHandler
                    ProxyDigestAuthHandler;  ProxyHandler; Request; URLopener;
                    UnknowHandler; build_opener; getproxies; install_opener; pathname2url;
                    url2pathname; urlcleanup; urlopen; urlretrieve;

urllib.response: addbase; addclosehook; addinfo; addinfourl;spa

urllib.robotparser: RobotFileParsercode

固然,python3.0的改動十分巨大,有須要能夠參閱change loghtm

若是想要學習python的話,仍是建議從3.0起步。由於畢竟時代是發展的,追隨最新的東西至少不是錯的。

相關文章
相關標籤/搜索