#coding:utf-8
import urllib2
import urllib
import httplib
import socket
import requests #实现以下几个方面内容:
##get请求,post请求
##请求参数自定义(querystring 针对get,form针对post,cookie,header)
##返回内容格式
##实现代理
def testforurllib():
r=urllib.urlopen('http://www.baidu.com')
#返回的内容
r.readline()
r.read()
r.info()
r.getcode()
r.geturl()
#get 加参数
params=urllib.urlencode({'name':'yy','age':22})#结果:name=yy$age=22
r1=urllib.urlopen('http://www.baidu.com?%s'%params)
#post 加参数
r2=urllib.urlopen('http://www.baidu.com',params)
print(r2.getcode())
#代理
proxies = {'http': 'http://127.0.0.1:7070/'}
opener=urllib.FancyURLopener(proxies)
opener.open('http://www.baidu.com')
print(opener.getcode())
#cookie实现比较没找到好的方法
pass
def testforurllib2():
#代理
proxy=urllib2.ProxyHandler({'http':'http://127.0.0.1:7070'})
opener=urllib2.build_opener(proxy)
#局部
opener.open('http://baidu.com')
##全局
urllib2.install_opener(opener)
urllib2.urlopen('bakdu.com') #get
urllib2.urlopen('http://cnblogs.com?%s'%urllib.urlencode({'page':2}))
#post
urllib2.urlopen('http://cnblogs.com',urllib.urlencode({'page':2})) #cookie
import cookielib
cj=cookielib.CookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
r=opener.open('http://weibo.com')
print(r.info())
#定制http头
r=urllib2.Request('http://cnblogs.com')
r.add_header('user-agent','xxx')
response=urllib2.urlopen(r)
pass
def testforhttplib():
#urllib是对httplib的封装,如果没有更精细的控制,使用urllib即可
#http://www.cnblogs.com/qq78292959/archive/2013/04/01/2993133.html
url='http://cnblogs.com'
params={'page':1} def testforrequests():
#这个api设置更爽
url='http://www.baidu.com'
params={'page':1}
r=requests.get(url)
r1=requests.post('http://httpbin.org/post')
#同理有put,delete,head,options
#添加参数
r3=requests.get(url,params=params) #获取响应
r4=requests.get('https://github.com/timeline.json')
print(r4.text+r4.encoding+str(r4.raw)) #添加post的data数据
import json
r5=requests.post('http://baidu.com',data={'page':1})
print(r5.status_code) #添加http头
headers={'a':'a'}
r6=requests.post('http://baidu.com',headers=headers)
print(r6.headers) #添加cookie
c=dict(a='a')
r7=requests.get('http://baidu.com',cookies=c)
print(len(r7.cookies)) #响应内容
r7.text
r7.content
r7.json()
r7.raw r7.status_code
r7.headers
r7.cookie['key']
r7.history
pass def main():
testforrequests()
pass main()

综上所述还是requests的api更好理解,使用起来也更简洁。

urllib,urllib2,requests对比的更多相关文章

  1. 人生苦短之Python的urllib urllib2 requests

    在Python中涉及到URL请求相关的操作涉及到模块有urllib,urllib2,requests,其中urllib和urllib2是Python自带的HTTP访问标准库,requsets是第三方库 ...

  2. 【Python爬虫实战--1】深入理解urllib;urllib2;requests

    摘自:http://1oscar.github.io/blog/2015/07/05/%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3urllib;urllib2;reques ...

  3. python中urllib, urllib2,urllib3, httplib,httplib2, request的区别

    permike原文python中urllib, urllib2,urllib3, httplib,httplib2, request的区别 若只使用python3.X, 下面可以不看了, 记住有个ur ...

  4. python urllib urllib2

    区别 1) urllib2可以接受一个Request类的实例来设置URL请求的headers,urllib仅可以接受URL.这意味着,用urllib时不可以伪装User Agent字符串等. 2) u ...

  5. Python 网络请求模块 urllib 、requests

    Python 给人的印象是抓取网页非常方便,提供这种生产力的,主要依靠的就是 urllib.requests这两个模块. urlib 介绍 urllib.request 提供了一个 urlopen 函 ...

  6. python中 urllib, urllib2, httplib, httplib2 几个库的区别

    转载 摘要: 只用 python3, 只用 urllib 若只使用python3.X, 下面可以不看了, 记住有个urllib的库就行了 python2.X 有这些库名可用: urllib, urll ...

  7. 浅谈urllib和requests

    urllib和requests的学习 urllib requests 参考资料 urllib urllib是python的基本库之一,内置四大模块,即request,error,parse,robot ...

  8. httplib urllib urllib2 pycurl 比较

    最近网上面试看到了有关这方面的问题,由于近两个月这些库或多或少都用过,现在根据自己的经验和网上介绍来总结一下. httplib 实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更 ...

  9. python通过get方式,post方式发送http请求和接收http响应-urllib urllib2

    python通过get方式,post方式发送http请求和接收http响应-- import urllib模块,urllib2模块, httplib模块 http://blog.163.com/xyc ...

随机推荐

  1. 详解x86、IA-32、IA-64等CPU系列

    x86架构起源于Intel公司在1978年推出的8086处理器.8086在1981年为IBM PC所选用,之后x86便成为了个人电脑的标准平台,成为历上最成功的CPU架构.8086是一款16位CPU, ...

  2. 生成1~n的全排列

    输入正整数n,输出n的全排列. 样例输入1: 3 样例输出1: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 分析: 按字典序从小到大的顺序输出所有的排列. (字典序:两个序 ...

  3. C#基础--面向过程计算器

    //面向过程计算器 //思路: 需要注意的是: 两个数相除 除数不能为0: //1.提示用户输入 //2.进行运算 //3.得到结果 Console.WriteLine("请输入第一个数字: ...

  4. eclipse安卓模拟器窗口大小调整

    引自百度经验的链接: http://jingyan.baidu.com/article/3aed632e18c7e97011809161.html

  5. 中文Ubuntu系统根目录文件夹名称变为英文

    Ubuntu中文安装后,家目录均为中文,如“下载” “文档”等等,在使用Shell时很不方便,可用如下方法将这些文件夹名称改回英文 1.使用命令 export LANG=en_US xdg-user- ...

  6. MongoDB索引、聚合

    用$where可以执行任意的js作为查询的一部分. db.foo.find({"$where" : function(){          for(var current in ...

  7. UNION语句查询(转载)

    联合查询   在对数据信息进行操作时,有时需要将不同数据表中的数据信息组合在一起,这时需要使用联合查询.联合查询指的是将多表中的行数据组合在一个数据集中进行显示.本节将讲解有关联合查询方面的相关知识. ...

  8. web字体

    <span style="font-family:sans-serif">Lorem Ipsum</span> <span style="f ...

  9. WCF之多个协定

    多个协定”示例演示如何在一个服务上实现多个协定,以及如何配置终结点以便与实现的每个协定进行通信 1.服务端代码如下(服务实现了两个协定,增加了黄色所示代码): class Program { stat ...

  10. Yocto 包管理 apt-get

    /******************************************************************** * Yocto 包管理 apt-get * 说明: * 查一 ...