Python网络编程中urllib2模块的用法总结
|
一、最基础的应用 import urllib2 url = r'http://www.baidu.com' html = urllib2.urlopen(url).read() print html 客户端与服务器端通过request与response来沟通,客户端先向服务端发送request,然后接收服务端返回的response urllib2提供了request的类,可以让用户在发送请求前先构造一个request的对象,然后通过urllib2.urlopen方法来发送请求 import urllib2 url = r'http://www.baidu.com' req = urllib2.Request(url) html = urllib2.urlopen(req).read() print html 上例中先使用 req = urllib2.Request(url) 实例化一个resquest对象,接下来使用 urllib2.urlopen(req) 来打开这个网页。 我们注意到在实例化Request对象的时候,队了url是必须的,还有几个默认的参数
基中data与header也是使用的比较多的,一些需要登录的才能浏览的网站经常需要这两个参数
import urllib
import urllib2
url = 'http://www.baidu.com/'
values = {'name' : 'Michael Foord','location' : 'Northampton','language' : 'Python' }
data = urllib.urlencode(values)
req = urllib2.Request(url,data)
response = urllib2.urlopen(req)
the_page = response.read()
print the_page
这个例子是向百度发送几个数据,这个例子是会返回一个错误页面,很正常,因为我们在访问百度的时候并不需要post什么信息,post了倒是会出错 百度是找不到相应的网页就会报错。 当然这个是POST数据,也可以用在GET方法,稍将上面的代码进行改造 百度是通过http://www.baidu.com/s?wd=XXX 来进行查询的,这样我们需要将{‘wd':'xxx'}这个字典进行urlencode
#coding:utf-8
import urllib
import urllib2
url = 'http://www.baidu.com/s'
values = {'wd':'杨彦星'}
data = urllib.urlencode(values)
print data
url2 = url+'?'+data
response = urllib2.urlopen(url2)
the_page = response.read()
print the_page
以下以模拟登录人人网然后再显示首页内容为例来详细说明一下cookie的使用,以下是文档中给的例子,我们就通过改造这个例子来实现我们想要的功能
import cookielib,urllib2
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
r = opener.open("http://example.com/")
#coding:utf-8
import urllib2,urllib
import cookielib
url = r'http://www.renren.com/ajaxLogin'
#创建一个cj的cookie的容器
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
#将要POST出去的数据进行编码
data = urllib.urlencode({"email":email,"password":pass})
r = opener.open(url,data)
print cj
当你看到有cj的时候,说明你已经访问了登录页面,是否正常登录你现在还看不出来,可以通过访问http://www.renren.com/home 来查看 上面的代码有两点要说明,我也是看了很长时间才明白 r = opener.open(url,data) 这句,为什么要使用opener这个对象来open,而不是用utllib2,urlopen?不光是例子里这么写,我们才这么写,通过改造我们也可以使用urllib2.urlopen,其实是因为opener是urllib2.bulid_opener创造出来的,但是你可以这样理解,他build出来后,自已却并没有安装使用它,也没有它的属性与方法,如果想使urllib2也具有opener的属性与方法,可以先使用urllib2.install_opener(opener)来"安装"这个opener,安装完以后就可以使用urllib2来操作了
#coding:utf-8
import urllib2,urllib
import cookielib
url = r'http://www.renren.com/ajaxLogin'
#创建一个cj的cookie的容器
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
#将要POST出去的数据进行编码
data = urllib.urlencode({"email":email,"password":pass})
#r = opener.open(url,data)如果没有上面的urllib2.install_opener方法,就必须这样写了
r = urllib2.urlopen(url,data)
html = urllib2.urlopen('http://www.renren.com/home').read()
print html
同样urllib2还有proxy相关的handle,基本的思路和这个差不多。 二、异常处理 当urlopen()不能处理响应时会引起URLError异常。HTTPError异常是URLError的一个子类,只有在访问HTTP类型的URL时才会引起。 1、URLError异常 通常引起URLError的原因是:无网络连接(没有到目标服务器的路由)、访问的目标服务器不存在。在这种情况下,异常对象会有reason属性(是一个(错误码、错误原因)的元组)。 #! /usr/bin/env python #coding=utf-8 import urllib2 url="http://www.baidu.com/" try: response=urllib2.urlopen(url) except urllib2.URLError,e: print e.reason 2、HTTPError #! /usr/bin/env python #coding=utf-8 import urllib2 url="http://cs.scu.edu.cn/~duanlei" try: response=urllib2.urlopen(url) except urllib2.HTTPError,e: print e.code print e.read() 3、总结 #! /usr/bin/env python #coding=utf-8 import urllib2 url="xxxxxx" #需要访问的URL try: response=urllib2.urlopen(url) except urllib2.HTTPError,e: #HTTPError必须排在URLError的前面 print "The server couldn't fulfill the request" print "Error code:",e.code print "Return content:",e.read() except urllib2.URLError,e: print "Failed to reach the server" print "The reason:",e.reason else: #something you should do pass #其他异常的处理 #! /usr/bin/env python #coding=utf-8 import urllib2 url="http://xxx" #需要访问的URL try: response=urllib2.urlopen(url) except urllib2.URLError,e: if hasattr(e,"reason"): print "Failed to reach the server" print "The reason:",e.reason elif hasattr(e,"code"): print "The server couldn't fulfill the request" print "Error code:",e.read() else: pass #其他异常的处理 相比较而言,第二种异常处理方法更优。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |

