#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. 发布Restful服务时出现IIS 指定了身份验证方案错误时的解决方案(IIS specified authentication schemes)

    发布RESTful服务,当访问.svc文件时出现如下错误时: IIS 指定了身份验证方案“IntegratedWindowsAuthentication, Anonymous”,但绑定仅支持一种身份验 ...

  2. C++使用POST方法向网页提交数据-----C++发送HTTP数据获取Google天气预报

    例1:C++使用POST方法向网页提交数据    转自:http://www.it165.net/pro/html/201208/3534.html 在C++中可以使用POST方法向网页中提交数据,这 ...

  3. USB peripherals can turn against their users

    Turning USB peripherals into BadUSB USB devices are connected to – and in many cases even built into ...

  4. MVC中Asp.Net管道(二)

    Asp.Net管道: 1.在工作进程w3wp.exe中,利用asp.net_isapi加载.NET运行时,6.0中引入了应用程序池的概念,一个工作进程对应的一个应用程序池.一个应用呢程序池可以加载一个 ...

  5. Android获取图片资源的4种方式

    1. 图片放在sdcard中 Bitmap imageBitmap = BitmapFactory.decodeFile(path) (path 是图片的路径,跟目录是/sdcard) 2. 图片在项 ...

  6. coreData旧版本增加字段,新版本是否可以继续使用旧版本内容的测试(MagicalRecord的使用)

    coreData使用第三方库MagicalRecord, 参考文章:http://blog.csdn.net/kuizhang1/article/details/21200367 coreData数据 ...

  7. 利用 Postfix 抵擋垃圾信

    reject_unknown_client               //add 20151117

  8. 使用RBL拦截垃圾邮件

    1.   sbl-xbl.spamhaus.org  2    bl.spamcop.net  3    zen.spamhaus.org

  9. HDU 5092

    http://acm.hdu.edu.cn/showproblem.php?pid=5092 卡读题,实质是每行取一个点,从上到下找一条路径权值和最小,点可以到达的地方是周围八个格子 类似数塔的dp, ...

  10. SQL基础,与数据类型;

    net start mssqlserver 开启数据库:  net stop mssqlserver 关闭数据库:  net pause mssqlserver 暂停数据库:  net continu ...