–博主提示:下面的是python2中的用法,python3需要做出相应修改。
urllib和urllib2模块都做与请求URL相关的操作,但他们提供不同的功能。
urllib2.urlopen accepts an instance of the Request class or a url, (whereas urllib.urlopen only accepts a url 中文意思就是:urllib2.urlopen可以接受一个Request对象或者url,(在接受Request对象时候,并以此可以来设置一个URL的headers),urllib.urlopen只接收一个url
urllib 有urlencode,urllib2没有,这也是为什么总是urllib,urllib2常会一起使用的原因
r = Request(url='http://www.mysite.com')
r.add_header('User-Agent', 'awesome fetcher')
r.add_data(urllib.urlencode({'foo': 'bar'})
response = urllib2.urlopen(r) #post method
I. Requests
使用的是 urllib3,继承了urllib2的所有特性。Requests支持HTTP连接保持和连接池,支持使用cookie保持会话,支持文件上传,支持自动确定响应内容的编码,支持国际化的 URL 和 POST 数据自动编码。 II. 举例:
import requests
...
resp = requests.get('http://www.mywebsite.com/user')
userdata = {"firstname": "John", "lastname": "Doe", "password": "jdoe123"}
resp = requests.post('http://www.mywebsite.com/user', params=userdata)
resp = requests.put('http://www.mywebsite.com/user/put')
resp = requests.delete('http://www.mywebsite.com/user/delete')
resp.json() # 假如返回的是json数据
resp.text #返回的不是text数据
resp.headers['content-type'] #返回text/html;charset=utf-8
f = open('request_index.html', 'w')
f.write(page.encode('utf8'))
#test 发现requests抓下来的页面必须要编码\
#写入,(抓下来的是unicode),urllib和urllib2抓下来可以直接写入,
#因为这两者抓下来的page是str
III. 其他功能特性
国际化域名和 URLs
Keep-Alive & 连接池
持久的 Cookie 会话
类浏览器式的 SSL 加密认证
基本/摘要式的身份认证
优雅的键/值 Cookies
自动解压
Unicode 编码的响应体
多段文件上传
连接超时
支持 .netrc
适用于 Python 2.6—3.4
线程安全
IV. requests不是python自带的库,需要另外安装 easy_install or pip install
V. requests缺陷:直接使用不能异步调用,速度慢(from others)。官方的urllib可以替代它。
VI. 个人不建议使用requests模块
urllib模块是python自带的,直接调用就好,用法如下:
1 #处理get请求,不传data,则为get请求 2 import urllib 3 from urllib.request import urlopen 4 from urllib.parse import urlencode 5 url='http://127.0.0.1:1990/login' 6 data={"username":"admin","password":123456} 7 req_data=urlencode(data)#将字典类型的请求数据转变为url编码 8 res=urlopen(url+'?'+req_data)#通过urlopen方法访问拼接好的url 9 res=res.read().decode()#read()方法是读取返回数据内容,decode是转换返回数据的bytes格式为str 10 print(res) 11 12 #处理post请求,如果传了data,则为post请求 13 import urllib 14 from urllib.request import urlopen 15 from urllib.request import Request 16 from urllib.parse import urlencode 17 url='http://127.0.0.1:1990/login' 18 data={"username":"admin","password":123456} 19 data=urlencode(data)#将字典类型的请求数据转变为url编码 20 data=data.encode('ascii')#将url编码类型的请求数据转变为bytes类型 21 req_data=Request(url,data)#将url和请求数据处理为一个Request对象,供urlopen调用 22 with urlopen(req_data) as res: 23 res=res.read().decode()#read()方法是读取返回数据内容,decode是转换返回数据的bytes格式为str 24 print(res)
相比较urllib模块,requests模块要简单很多,具体用法如下:
1 # get请求 2 import requests 3 url='http://127.0.0.1:1990/login' 4 data={"username":"admin","password":123456} 5 res=requests.get(url,data)#直接用requests.get(url,data)即可,其中.get表示为get方法,不需要对字典类型的data进行处理 6 #res=res.text#text方法是获取到响应为一个str,也不需要对res进行转换等处理 7 res=res.json()#当返回的数据是json串的时候直接用.json即可将res转换成字典 8 print(res) 9 10 #post请求 11 import requests 12 url='http://127.0.0.1:1990/login' 13 data={"username":"admin","password":123456} 14 res=requests.post(url,data)#直接用requests.post(url,data)即可,其中.post表示为post方法,不需要对字典类型的data进行处理 15 #res=res.text#text方法是获取到响应为一个str,也不需要对res进行转换等处理 16 res=res.json()#当返回的数据是json串的时候直接用.json即可将res转换成字典 17 print(res) 18 19 #当传参格式要求为json串时 20 import requests 21 url='http://127.0.0.1:1990/login' 22 data={"username":"admin","password":123456} 23 res=requests.post(url,json=data)#只需要在这里指定data为json即可 24 #res=res.text#text方法是获取到响应为一个str,也不需要对res进行转换等处理 25 res=res.json()#当返回的数据是json串的时候直接用.json即可将res转换成字典 26 print(res) 27 28 #传参含cookie 29 import requests 30 url='http://127.0.0.1:1990/login' 31 data={"username":"admin","password":123456} 32 cookie={"sign":"123abc"} 33 res=requests.post(url,json=data,cookies=cookie)#只需要在这里指定cookies位cookie即可,headers,files等类似 34 res=res.json() 35 print(res)