python3.x中的urllib模块

最新版的python3.3.0已经发布了。相较于python3.0,3.2的改动并不大。但网上的大量的教程等大都以2.x版本为基础。这为想要从python3.0学起的菜鸟带来了不少的困难。 作为一只菜鸟,最近想学习一下Python中urllib模块的使用方法。从网上找的最简单的实例:把google 首页的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中。 原文是这样写的:

a new urllib package was created. It consists of code from 
urlliburllib2urlparse, and robotparser. The old 
modules have all been removed. The new package has five submodules: 
urllib.parseurllib.requesturllib.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、和robotparser并入了urllib中,并且修改urllib模块,其中包含5个子模块,即是help()中看到的那五个名字。

为了今后使用方便,在此将每个包中包含的方法列举如下: urllib.error: ContentTooShortError; HTTPError; URLError

urllib.parse: parseqs; parseqsl; quote; quotefrombytes; quote_plus; unquote unquoteplus; unquoteto_bytes; urldefrag; urlencode; urljoin; urlparse; urlsplit; urlunparse; urlunsplit

urllib.request: AbstractBasicAuthHandler; AbstractDigestAuthHandler; BaseHandler; CatheFTPHandler; FTPHandler; FancyURLopener; FileHandler; HTTPBasicAuthHandler; HTTPCookieProcessor; HTTPDefaultErrorHandler; HTTPDigestAuthHandler; HTTPErrorProcessorl; HTTPHandler; HTTPPasswordMgr; HTTPPasswordMgrWithDefaultRealm; HTTPRedirectHandler; HTTPSHandler;OpenerDirector;ProxyBasicAuthHandler ProxyDigestAuthHandler; ProxyHandler; Request; URLopener; UnknowHandler; buildopener; getproxies; installopener; pathname2url; url2pathname; urlcleanup; urlopen; urlretrieve;

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

urllib.robotparser: RobotFileParser


有朋友在执行

import urllib

 html = urllib.request.urlopen('www.baidu.com')

print(html.read())

会报一个错误提示无此方法

  这是因为python3特性导致,需要 import urllib.request

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值