#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. 一篇很好介绍stringBuffer和StringBuilder的区别--来自百度

    ava.lang.StringBuffer线程安全的可变字符序列.一个类似于 String 的字符串缓冲区,但不能修改.虽然在任意时间点上它都包含某种特定的字符序列,但通过某些方法调用可以改变该序列的 ...

  2. (转)SQLite数据库增删改查操作

    原文:http://www.cnblogs.com/linjiqin/archive/2011/05/26/2059182.html SQLite数据库增删改查操作 一.使用嵌入式关系型SQLite数 ...

  3. Python中的闭包

    简单的闭包的栗子: def counter(statr_at = 0): count = 1 def incr(): nonlocal count #注意由于count类型为immutable,所以需 ...

  4. Java中的接口与抽象类

    抽象类很简单,就是多了个abstract关键字,可以有(也可以没有)只声明不定义的方法.不能实例化该类. 接口比较特殊: 无论你加不加public,接口中声明的方法都是public的,还有无论你加不加 ...

  5. linux常用命令:4文件压缩和解压命令

    文件压缩和解压命令 压缩命令:gzip.tar[-czf].zip.bzip2 解压缩命令:gunzip.tar[-xzf].unzip.bunzip2 1. 命令名称:gzip 命令英文原意:GNU ...

  6. 解决Xcode7.1插件安装的办法

    现象一. 运行安装后,没有出现在菜单上. 1. 到githup上下载Alcatraz project https://github.com/supermarin/Alcatraz2. 打开终端 3. ...

  7. ios openURL的使用(调用系统电话、浏览器、地图、邮件等)

    Safari Any URL starting with http:// which does not point to maps.google.com or www.youtube.com is s ...

  8. Postfix之sasldb2

    # 2.#配置postfix启用sasldb2作为smtp的账号秘密效验方式 3.#编辑通过sasl启用smtp账号密码效验的配置 4.vi /etc/sasl2/smtpd.conf #vi写入或编 ...

  9. SQUID之cache_peer

    一共关系到cache_peer/always_direct/never_direct/hierarchy_stoplist/prefer_direct等配置项. squid的使用指南上,关于alway ...

  10. Channel Allocation_四色定理

    Description When a radio station is broadcasting over a very large area, repeaters are used to retra ...