urllib,urllib2,requests对比
#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对比的更多相关文章
- 人生苦短之Python的urllib urllib2 requests
在Python中涉及到URL请求相关的操作涉及到模块有urllib,urllib2,requests,其中urllib和urllib2是Python自带的HTTP访问标准库,requsets是第三方库 ...
- 【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 ...
- python中urllib, urllib2,urllib3, httplib,httplib2, request的区别
permike原文python中urllib, urllib2,urllib3, httplib,httplib2, request的区别 若只使用python3.X, 下面可以不看了, 记住有个ur ...
- python urllib urllib2
区别 1) urllib2可以接受一个Request类的实例来设置URL请求的headers,urllib仅可以接受URL.这意味着,用urllib时不可以伪装User Agent字符串等. 2) u ...
- Python 网络请求模块 urllib 、requests
Python 给人的印象是抓取网页非常方便,提供这种生产力的,主要依靠的就是 urllib.requests这两个模块. urlib 介绍 urllib.request 提供了一个 urlopen 函 ...
- python中 urllib, urllib2, httplib, httplib2 几个库的区别
转载 摘要: 只用 python3, 只用 urllib 若只使用python3.X, 下面可以不看了, 记住有个urllib的库就行了 python2.X 有这些库名可用: urllib, urll ...
- 浅谈urllib和requests
urllib和requests的学习 urllib requests 参考资料 urllib urllib是python的基本库之一,内置四大模块,即request,error,parse,robot ...
- httplib urllib urllib2 pycurl 比较
最近网上面试看到了有关这方面的问题,由于近两个月这些库或多或少都用过,现在根据自己的经验和网上介绍来总结一下. httplib 实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更 ...
- python通过get方式,post方式发送http请求和接收http响应-urllib urllib2
python通过get方式,post方式发送http请求和接收http响应-- import urllib模块,urllib2模块, httplib模块 http://blog.163.com/xyc ...
随机推荐
- 《day13--异常的进阶和包的使用》
//101-finally的使用&102-finally的使用场景 /* 需求:有一些特定的代码,无论异常是否发生,都需要执行, 因为异常会引发程序的跳转,导致有些语句执行不到,无法满足这个需 ...
- Android布局— — —线性布局
以水平或垂直的方式显示界面中的控件 线性布局 语法格式: <LinearLayout xmlns:android="http://schemas.android.com/apk/res ...
- postgreSQL9.1忘记postgres用户密码怎么办
在网络上找了一篇文章http://www.linuxidc.com/Linux/2010-04/25232.htm,如下: Ubuntu 9.10下PostgreSQL 8.4忘记密码的解决方法 Ub ...
- 统计文件夹下java代码行数的小程序--主要是学习任务队列的思想
首先感谢czbk的老师,录制的视频,让我们有这么好的学习资料.……—— 统计文件夹java文件的行数,首先想到的肯定是用递归的方法,因为文件夹下面可能包含文件夹,用递归的方法,代码容易写.(这和写简单 ...
- application:didFinishLaunchingWithOptions:详解
iOS 程序启动时总会调用application:didFinishLaunchingWithOptions:,其中第二个参数launchOptions为NSDictionary类型的对象,里面存储有 ...
- java字节数组和16进制之间的转换
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ pac ...
- PHP中的文件系统处理(一)
PHP文件系统处理 所有文件处理都是使用系统函数完成的. 是基于Linux/Unix系统为模型 文件系统处理的作用: 1. 所有的项目离不开文件处理 ...
- Magento后台表单字段添加备注
Magento的后台表单封装的非常好,各种字段都能够直接找到方法调用.在最近的一个项目中,为客户定制了一款定时变价功能,该功能需要导入一个csv作为变价的基础.为了方便客户,我们需要在上传表单位置添加 ...
- js 字符串转化成数字:(实例:用正则检测大于0的正数,最多保留4位小数)
来源:http://www.cnblogs.com/hwx0807/archive/2011/06/28/2092021.html 实例: function BindSubmitEvent() { / ...
- timeZoneGetter
function timeZoneGetter(date) { // getTimezoneOffset 返回格林威治时间和本地时间之间的时差,以分钟为单位 var zone = -1 * date. ...