python的requests初步使用
来源:互联网 发布:linux 打包有什么用 编辑:程序博客网 时间:2024/04/30 01:39
早就听说requests的库的强大,只是还没有接触,今天接触了一下,发现以前使用urllib,urllib2等方法真是太搓了……
这里写些简单的使用初步作为一个记录
安装
http://cn.python-requests.org/en/latest/user/install.html#install
发送无参数的get请求
1234567891011121314151617181920212223242526
r = requests.get('http://httpbin.org/get')print r.text'''{ "args": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Host": "httpbin.org", "User-Agent": "python-requests/2.3.0 CPython/2.6.6 Windows/7", "X-Request-Id": "8a28bbea-55cd-460b-bda3-f3427d66b700" }, "origin": "124.192.129.84", "url": "http://httpbin.org/get"'''<!-- more --># 发送带参数的get请求,将key与value放入一个字典中,通过params参数来传递,其作用相当于urllib.urlencode``` python>>> import requests>>> pqyload = {'q':'杨彦星'}>>> r = requests.get('http://www.so.com/s',params = pqyload)>>> r.urlu'http://www.so.com/s?q=%E6%9D%A8%E5%BD%A6%E6%98%9F'
发送post请求,通过data参数来传递,
123456789101112131415161718192021222324252627
>>> payload = {'a':'杨','b':'hello'}>>> r = requests.post("http://httpbin.org/post", data=payload)>>> print r.text{ "args": {}, "data": "", "files": {}, "form": { "a": "u6768", "b": "hello" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Content-Length": "19", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.3.0 CPython/2.6.6 Windows/7", "X-Request-Id": "c81cb937-04b8-4a2d-ba32-04b5c0b3ba98" }, "json": null, "origin": "124.192.129.84", "url": "http://httpbin.org/post"}>>>
可以看到,post参数已经传到了form里,data不光可以接受字典类型的数据,还可以接受json等格式
1234
>>> payload = {'a':'杨','b':'hello'}>>> import json>>> r = requests.post('http://httpbin.org/post', data=json.dumps(payload))
发送文件的post类型
这个相当于向网站上传一张图片,文档等操作,这时要使用files参数
1234
>>> url = 'http://httpbin.org/post'>>> files = {'file': open('touxiang.png', 'rb')}>>> r = requests.post(url, files=files)
定制headers,使用headers参数来传递
1234567
>>> import json>>> url = 'https://api.github.com/some/endpoint'>>> payload = {'some': 'data'}>>> headers = {'content-type': 'application/json'}>>> r = requests.post(url, data=json.dumps(payload), headers=headers)
响应内容
响应状态码
r = requests.get(‘http://httpbin.org/get‘)
print r.status_code
响应头
123
>>> print r.headers{'content-length': '519', 'server': 'gunicorn/18.0', 'connection': 'keep-alive', 'date': 'Sun, 15 Jun 2014 14:19:52 GMT', 'access-control-allow-origin': '*', 'content-type': 'application/json'}
也可以取到这个个别的响应头用来做一些判断,这里的参数是不区分大小写的
r.headers[‘Content-Type’]
r.headers.get(‘Content-Type’)
响应内容,前面已经在应用了
r.text
r.content
获取响应中的cookies
1234
>>> r = requests.get('http://www.baidu.com')>>> r.cookies['BAIDUID']'D5810267346AEFB0F25CB0D6D0E043E6:FG=1'
也可以自已定义请求的COOKIES
123456789101112
>>> url = 'http://httpbin.org/cookies'>>> cookies = {'cookies_are':'working'}>>> r = requests.get(url,cookies = cookies)>>>>>> print r.text{ "cookies": { "cookies_are": "working" }}>>>
cookies还有很多,因为目前我也还不是很多,以后再扩充吧
使用timeout参数设置超时时间
12
>>> requests.get('http://github.com', timeout=1)<Response [200]>
如果将时间设置成非常小的数,如requests.get(‘http://github.com’, timeout=0.001),那么如果在timeout的时间内没有连接,那么将会抛出一个Timeout的异常
访问中使用session
先初始化一个session对象,s = requests.Session()
然后使用这个session对象来进行访问,r = s.post(url,data = user)
参考文章 http://blog.csdn.net/iloveyin/article/details/21444613 基本上都是从这扒的代码
以下通过访问人人网来获取首页中的最近来访问,然后再访问查看更多的来访来读取更多的最近来访
20151029更新,和美女刘巍进行讨论,由于很久没有登录人人网,它的登录页面与获得最近来访的页面都有所变动,登录时的url是http://www.renren.com/ajaxLogin/login 获取最近来访是http://www.renren.com/myfoot/whoSeenMe
更多的来访就是以带session的访问http://www.renren.com/myfoot/whoSeenMe
123456789101112131415161718192021222324252627282930
#coding:utf-8import requestsimport reurl = r'http://www.renren.com/ajaxLogin/login'user = {'email':'email','password':'pass'}s = requests.Session()r = s.post(url,data = user)html = r.textvisit = []first = re.compile(r'</span><span class="time-tip first-tip"><span class="tip-content">(.*?)</span>')second = re.compile(r'</span><span class="time-tip"><span class="tip-content">(.*?)</span>')third = re.compile(r'</span><span class="time-tip last-second-tip"><span class="tip-content">(.*?)</span>')last = re.compile(r'</span><span class="time-tip last-tip"><span class="tip-content">(.*?)</span>')visit.extend(first.findall(html))visit.extend(second.findall(html))visit.extend(third.findall(html))visit.extend(last.findall(html))for i in visit: print iprint '以下是更多的最近来访'vm = s.get('http://www.renren.com/myfoot/whoSeenMe')fm = re.compile(r'"name":"(.*?)"')visitmore = fm.findall(vm.text)for i in visitmore: print i
- python的requests初步使用
- python的requests初步使用
- python的requests初步使用
- python的requests初步使用
- Python-requests的使用
- python的 requests库使用
- Python库Requests的基础使用
- python HTTP请求--requests 的使用
- 使用Python的Requests包模拟登陆
- python-requests库的基本使用教程
- python中的requests使用
- python的requests库
- python 的requests模块
- python requests的方法
- 使用 python requests 模拟登陆
- Python requests库使用实例
- python使用requests获取cookie
- 使用python的requests 发送multipart/form-data 请求
- struts2查看源码
- git 彻底返回上一次提交前的版本
- hrbust 1424 哈理工oj 1424 Hrbust的校车【水题】【思维】
- bootstrap根据数据库自动生成多列
- VLC-SDK调用学习
- python的requests初步使用
- 安卓调用手机的相册与相机获取图片并适配到控件
- 后台时间返回前台变一串数字
- hibernate setflushmode
- 关于iOS TableView旋转后头部空白的问题
- android bitmap设置图片内存溢出的解决神器
- 四个作用域
- 逆序对解决方法之归并排序
- iOS开发之使用UIWebView实现图文混排