Python urllib模块与urlopen()函数解析
来源:互联网 发布:如何引出大数据 编辑:程序博客网 时间:2024/05/19 05:33
Python urllib 库提供了一个从指定的 URL 地址获取网页数据,然后对其进行分析处理,获取想要的数据。
下面是在 Python Shell 里的 urllib 的使用情况:
Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import urllib
>>> google = urllib.urlopen('http://www.google.com')
>>> print 'http header:\n',google.info()
http header:
Date: Wed, 30 Oct 2013 03:11:44 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=Big5
Set-Cookie: PREF=ID=7ee0cbd58be6fb74:FF=0:NW=1:TM=1383102704:LM=1383102704:S=w6DoLuUBc7KUOE69; expires=Fri, 30-Oct-2015 03:11:44 GMT; path=/; domain=.google.com.hk
Set-Cookie: NID=67=cNyh4vZeoDJFnSe12viwoMNh
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 80:quic
>>> print 'http status:',google.getcode()
http status: 200
>>> print 'url:',google.geturl()
url: http://www.google.com.hk/
>>>
上面主要用到了 urllib 库里的 urlopen() 函数。我们可以了解一下这个函数。
继续使用 Python Shell:
>>> help(urllib.urlopen)
Help on function urlopen in module urllib:
urlopen(url, data=None, proxies=None)
即创建一个类文件对象为指定的 url 来读取。
详细点就是,创建一个表示远程url的类文件对象,然后像本地文件一样操作这个类文件对象来获取远程数据。参数url表示远程数据的路径,一般是网址;参数data表示以post方式提交到url的数据(玩过web的人应该知道提交数据的两种方式:post与get。如果你不清楚,也不必太在意,一般情况下很少用到这个参数);参数proxies用于设置代理(这里不详细讲怎么使用代理,感兴趣的看客可以去翻阅Python手册urllib模块)。 urlopen返回 一个类文件对象,他提供了如下方法:
- 参数 url 表示远程数据的路径,一般是 http 或者 ftp 路径。
- 参数 data 表示以 get 或者 post 方式提交到 url 的数据。
- 参数 proxies 表示用于代理的设置。
- urlopen 返回一个类文件对象,它提供了如下方法:
- read() , readline() , readlines(),fileno()和close(): 这些方法的使用与文件对象完全一样。
- info():返回一个httplib.HTTPMessage 对象,表示远程服务器返回的头信息。
- getcode():返回Http状态码,如果是http请求,200表示请求成功完成;404表示网址未找到。
- geturl():返回请求的url地址。
再看一个例子,这个例子把Google首页的html抓取下来并显示在控制台上:
# 别惊讶,整个程序确实只用了两行代码 import urllib print urllib.urlopen('http://www.google.com').read()再运行一下这个例子,以加深对urllib的印象:google = urllib.urlopen('http://www.google.com')print 'http header:/n', google.info() print 'http status:', google.getcode() print 'url:', google.geturl() for line in google: # 就像在操作本地文件 print line, google.close()
- Python urllib模块与urlopen()函数解析
- Python urllib模块与urlopen()函数解析
- Python urllib模块与urlopen()函数解析偎
- Python urllib模块与urlopen()函数解析疵
- Python urllib模块urlopen()与urlretrieve()详解
- Python urllib模块urlopen()与urlretrieve()详解
- (转)Python urllib模块urlopen()与urlretrieve()详解
- Python 3.6模块学习urllib的urllib.request.urlopen()函数学习
- Python urllib模块urlopen()【打开网站,写入本地文件】与urlretrieve()【直接下载网站文件到本地】
- python-urllib简介【urlopen()、urlretrieve()、编解码】
- Python3:urllib中urlopen()函数新特点
- Python3:urllib中urlopen()函数新特点
- [Note]关于Python中文编码出现乱码的解决方案(涉及urllib.urlopen(),open()等函数)
- Python urllib的urlretrieve()函数解析
- Python urllib的urlretrieve()函数解析
- Python urllib的urlretrieve()函数解析
- Python:urllib与urllib2错误解析
- python中的urllib模块
- SQL多表操作、插入、更新、删除(mysql)
- mongodb性能优化
- shell中$0,$?,$!等的特殊用法
- 37-使用 kill 函数发送信号
- 如何使用指针从函数返回一个数组。
- Python urllib模块与urlopen()函数解析
- CentOS7 (精简操作指令)
- 汉诺塔问题
- 输出星号菱形
- MySQL详解--锁
- 取近似值
- Hadoop HA 高可用平台配置实例
- CSS:position属性
- 替换空格