#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. STM32之延时秒,毫秒,微秒

    #include "delay.h" #include "stdint.h" #include "stm32f10x.h" ; //us延时 ...

  2. JavaScript之document对象使用

    1.document 对象常用的有三种: A.document.getElementById:通过html元素的Id,来获取html对象.适用于单个的html元素. B.document.getEle ...

  3. Android布局---相对布局

    Android布局分为五大类:相对布局.线性布局.表格布局.帧布局.网格布局 相对布局 语法格式: <RelativeLayout xmlns:android="http://sche ...

  4. EasyUI 验证框使用方法

    使用过程中的一积累,备查. EasyUI 验证框使用方法://***************************missingMessage:未填写时显示的信息validType:验证类型见下示例 ...

  5. C++中的::operator new, ::operator delete

    一般在使用new  和 delete的时候,做了两件事情,一是空间的配置( new 是分配,delete是回收),而是调用对象的析构函数 但是也有办法将这两个过程分开 那就是显式的调用::operat ...

  6. php生成图片注释

    //生成验证码图片注释 <?php session_start(); $arr = array( 'a','b','c','d','e','f','g','h','i','j','k','l', ...

  7. gen already exists but is not a source folder ZT

    解决方法:1. 右键点击工程,选择 "Properties"2. 选择左边的 "Java Build Path" 3. 打开 "Source" ...

  8. Java 集合深入理解(11):LinkedList

    点击查看 Java 集合框架深入理解 系列, - ( ゜- ゜)つロ 乾杯~ 今天心情鱼肚白,来学学 LinkedList 吧! 日常开发中,保存一组数据使用的最多的就是 ArrayList, 其次就 ...

  9. Inno Setup 插件大全

    Inno Setup 插件大全     这是我收集到的目前网上最全的插件之一,里面的每个插件,都有详细的脚本示例来讲解该插件的具体用法.另外,下载了我公开的脚本的朋友,也有可能会被提示缺少文件,如果缺 ...

  10. Disable Portrait in app

    I had this problem as well as I wanted to constrain my game to only landscape mode. I put this in my ...