Python urllib 库提供了一个从指定的 URL 地址获取网页数据,然后对其进行分析处理,获取想要的数据。

下面是在 Python Shell 里的 urllib 的使用情况:

01 Python 2.7.5 (default, May 15 201322:44:16) [MSC v.1500 64 bit (AMD64)] on win32
02 Type "copyright""credits" or "license()" for more information.
03 >>> import urllib
04 >>> google = urllib.urlopen('http://www.google.com')
05 >>> print 'http header:\n',google.info()
06 http header:
07 Date: Wed, 30 Oct 2013 03:11:44 GMT
08 Expires: -1
09 Cache-Control: private, max-age=0
10 Content-Type: text/html; charset=Big5
11 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
12 Set-Cookie: NID=67=cNyh4vZeoDJFnSe12viwoMNh47Hjq98F72I6TTNZGBuJx78aRgQbAA-RtGNFFpARCaN3zJ6OYIpJASB3Q7cmfyRguFh6epcBOSL930KEfIxUa-85e946hE97WfP0lgk7; expires=Thu, 01-May-2014 03:11:44 GMT; path=/; domain=.google.com.hk; HttpOnly
13 P3P: CP="This is not a P3P policy! Seehttp://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657for more info."
14 Server: gws
15 X-XSS-Protection: 1; mode=block
16 X-Frame-Options: SAMEORIGIN
17 Alternate-Protocol: 80:quic
18  
19 >>> print 'http status:',google.getcode()
20 http status: 200
21 >>> print 'url:',google.geturl()
22 url: http://www.google.com.hk/
23 >>>

上面主要用到了 urllib 库里的 urlopen() 函数。我们可以了解一下这个函数。

继续使用 Python Shell:

1 >>> help(urllib.urlopen)
2 Help on function urlopen in module urllib:
3  
4 urlopen(url, data=None, proxies=None)
5     Create a file-like object for the specified URL to read from.

即创建一个类文件对象为指定的 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抓取下来并显示在控制台上:

1 # 别惊讶,整个程序确实只用了两行代码 
2 import urllib 
3 print urllib.urlopen('http://www.google.com').read()

再运行一下这个例子,以加深对urllib的印象:

1 google = urllib.urlopen('http://www.google.com'
2 print 'http header:/n', google.info() 
3 print 'http status:', google.getcode() 
4 print 'url:', google.geturl() 
5 for line in google: # 就像在操作本地文件 
6     print line, 
7 google.close() 

【py网页】urllib模块,urlopen的更多相关文章

  1. Python基础之 urllib模块urlopen()与urlretrieve()的使用方法详解。

    Python urllib模块urlopen()与urlretrieve()的使用方法详解   1.urlopen()方法urllib.urlopen(url[, data[, proxies]]) ...

  2. Python urllib模块urlopen()与urlretrieve()详解

    1.urlopen()方法urllib.urlopen(url[, data[, proxies]]) :创建一个表示远程url的类文件对象,然后像本地文件一样操作这个类文件对象来获取远程数据.参数u ...

  3. python urllib模块的urlopen()的使用方法及实例

    Python urllib 库提供了一个从指定的 URL 地址获取网页数据,然后对其进行分析处理,获取想要的数据. 一.urllib模块urlopen()函数: urlopen(url, data=N ...

  4. 【py网页】urlopen的补充,完美

    urllib 是 python 自带的一个抓取网页信息一个接口,他最主要的方法是 urlopen(),是基于 python 的 open() 方法的.下面是主要说明: 1 urllib.urlopen ...

  5. 定义一个方法get_page(url),url参数是需要获取网页内容的网址,返回网页的内容。提示(可以了解python的urllib模块)

    定义一个方法get_page(url),url参数是需要获取网页内容的网址,返回网页的内容.提示(可以了解python的urllib模块) import urllib.request def get_ ...

  6. 【py网页】urllib.urlretrieve远程下载

    下面我们再来看看 urllib 模块提供的 urlretrieve() 函数.urlretrieve() 方法直接将远程数据下载到本地. 1 >>> help(urllib.urlr ...

  7. 12月4日学习爬虫007.使用Urllib模块进行简单网页爬取

    笔记如下: 1.https是http加强版协议(安全协议)http(普通网络通信协议) 爬数据 如果爬https发现和理想中的数据不同,可以改为http 直接去掉s即可 2.使用Urllib爬取简单网 ...

  8. 全局变量 urllib模块 json模块

    1.vars()  查看一个.py文件中的全局变量 print(vars()) #重点 __name__': '__main__ '__file__': 'C:/Users/lenovo/Pychar ...

  9. Python第十三天 django 1.6 导入模板 定义数据模型 访问数据库 GET和POST方法 SimpleCMDB项目 urllib模块 urllib2模块 httplib模块 django和web服务器整合 wsgi模块 gunicorn模块

    Python第十三天   django 1.6   导入模板   定义数据模型   访问数据库   GET和POST方法    SimpleCMDB项目   urllib模块   urllib2模块 ...

随机推荐

  1. Emiller's Advanced Topics In Nginx Module Development

    Emiller的Nginx模块开发指南 By Evan Miller DRAFT: August 13, 2009 (changes) 翻译:Kongch @2010年1月5日 0:04am -- 2 ...

  2. Magento - Rewrite机制一窥

    看一个url例子 http://localhost/magento/index.php/customer/account/login 这里假定http://localhost/magento/ 是ma ...

  3. Selenium2学习-000-Selenium2初识

    什么是 Selenium?Selenium 是 ThoughtWorks 专门为 Web 应用程序编写的一个验收测试工具,是一种 Web 测试框架,开拓了验证 Web 应用程序的新方案,使您可在 We ...

  4. php object转数组示例

    原本是这样格式的数据: object(Thrift\Server\PageCards)#32 (3) { ["cards"]=> array(10) { [0]=> o ...

  5. LintCode Kth Largest Element

    原题链接在这里:http://www.lintcode.com/en/problem/kth-largest-element/# 在LeetCode上也有一道,采用了标准的quickSelect 方法 ...

  6. Linux 源码安装httpd

    安装apr 下载解压apr-1.4.5 ./configure --prefix=/usr/local/apr make sudo make install 安装apr-util 下载解压apr-ut ...

  7. javaee学习之servlet

    一.tomcat相关知识 tomecat虚拟主机与虚拟路径 1.tomcat的应用默认放在webapps目录下面,可以将其放在其他目录分区,让tomcat进行管理吗? 答:当然可以.方法:配置虚拟目录 ...

  8. category分类

    /* 使用继承关系来扩充一个类,有一个弊病,高耦合性 category(分类,类别) 能够帮我们扩充一个类的功能 */ - (void)superJump { //    [self eat]; [s ...

  9. Go 性能分析

    上线一定要用压力测试,才能知道自己的承受度是多少,不然出了问题,就各种排查. http://www.tuicool.com/articles/NVRJrm 通过jmeter压力测试,发现打印请求参数消 ...

  10. CSS浮动与清浮动

    浮动 ( float css属性) float : left right Elements are floated horizontally, this means that an element c ...