python第三方库系列之二十四--http-web库
来源:互联网 发布:手机淘宝差评怎么删 编辑:程序博客网 时间:2024/05/18 04:59
共推荐6种库,强烈推荐requests库。
web库之一:httplib库
#!/usr/bin/env python#coding=utf8import httplibhttpClient = Nonetry: httpClient = httplib.HTTPConnection('www.baidu.com', 80, timeout=30) httpClient.request('GET', '/') #response是HTTPResponse对象 response = httpClient.getresponse() print response.status print response.reason print response.read()except Exception, e: print efinally: if httpClient: httpClient.close()
web库之二:urllib库
python更高层的封装模块中(urllib,urllib2)使用了http实现。
# coding=utf8import urllibbaidu = urllib.urlopen('http://www.baidu.com')print baidu.read()print 'http header:/n', baidu.info()print 'http status:', baidu.getcode()print 'url:', baidu.geturl()for line in baidu: # 就像在操作本地文件 print line,baidu.close()
web库之三:urllib2库--basic and digest authentication, redirections, cookies and more.
#coding:utf-8import urllib import urllib2 url = 'http://www.baidu.com/s' values = {'wd':'杨彦星'} data = urllib.urlencode(values)print data url2 = url+'?'+dataresponse = urllib2.urlopen(url2) the_page = response.read()print the_pageweb库之四:cookielib库
#coding:utf-8import urllib2,urllibimport cookieliburl = 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来操作了
web库之五:httplib2库
1.http.client 是实现了rfc 2616, http 协议的底层库;
2.urllib.request 建立在http.client之上一个抽象层。 它为访问http 和 ftp 服务器提供了一个标准的api,可以自动跟随http 重定向, 并且处理了一些常见形式的http 认证;
3.httplib2是一个第三方的开源库,它比http.client更完整的实现了http协议,同时比urllib.request提供了更好的抽象。
#coding=utf8import httplib2#获取HTTP对象h =httplib2.Http()#发出同步请求,并获取内容resp, content = h.request("http://www.weirdbird.net/")print respprint content
httplib2包括这些功能:httplib2缓存处理,httplib2处理Last-Modified和ETag头,http2lib 处理压缩,httplib2 处理重定向等。
web库之六:requests库
官方网址:http://www.python-requests.org/en/latest/
- python第三方库系列之二十四--http-web库
- python第三方库系列之二十六--virtualenv库
- python第三方库系列之二十三--路径库
- python第三方库系列之二--time库
- python第三方库系列之二十五--pip库
- python第三方库系列之十四--集群化部署定时任务apscheduler库
- python第三方库系列之二十一--python django的权限管理库
- python第三方库系列之十六--建立最简单的web服务器
- python第三方库系列之二十--批量机器配置管理库SaltStack
- python第三方库系列之二十二--subprocess的用法
- python第三方库系列之二十六--不知道被谁删了微信好友
- python第三方库系列之六--traceback库
- python第三方库系列之七--logging库
- python第三方库系列之八--uuid库
- python第三方库系列之九--os库
- python第三方库系列之十--commands库
- python第三方库系列之十二--多线程threading库
- python第三方库系列之十五--编码库
- slony I 数据复制工具的安装与应用
- 安全联盟认证登陆QQ浏览器,中网认证被取代
- oracle删除(释放)数据文件/表空间流程
- decreases inversion count
- 第七周项目6人数不定的工资类
- python第三方库系列之二十四--http-web库
- ASP.NET的web.config
- Spring AOP 的简单例子
- Codeforces B. Taxi 算法题解
- windows服务等获取文件路径文件目录方法
- MVC设计模式
- linux zip/unzip命令
- d3的scale()函数
- (code jam)Problem B. Cookie Clicker Alpha