最近

有些朋友

看完小帅b的文章之后

把小帅b的表情包都偷了

还在我的微信

疯狂发表情包嘚瑟

我就呵呵了

只能说一句

盘他

还有一些朋友

看完文章不点好看

还来催更

小帅b也只能说一句

继续盘他

 

ok

接下来我们要来玩一个新的库

这个库的名称叫做

Requests

这个库比我们上次说的 urllib 可是要牛逼一丢丢的

毕竟 Requests 是在 urllib 的基础上搞出来的

通过它我们可以用更少的代码

模拟浏览器操作

人生苦短

接下来就是

学习 python 的正确姿势

 

skr

对于不是 python 的内置库

我们需要安装一下

直接使用 pip 安装

pip install requests

 

安装完后就可以使用了

接下来就来感受一下 requests 吧

导入 requests 模块

import requests

一行代码 Get 请求

r = requests.get('https://api.github.com/events')

一行代码 Post 请求

r = requests.post('https://httpbin.org/post', data = {'key':'value'})

 

其它乱七八糟的 Http 请求

>>> r = requests.put('https://httpbin.org/put', data = {'key':'value'})

>>> r = requests.delete('https://httpbin.org/delete')

>>> r = requests.head('https://httpbin.org/get')

>>> r = requests.options('https://httpbin.org/get')

想要携带请求参数是吧?

>>> payload = {'key1': 'value1', 'key2': 'value2'}

>>> r = requests.get('https://httpbin.org/get', params=payload)

假装自己是浏览器

>>> url = 'https://api.github.com/some/endpoint'

>>> headers = {'user-agent': 'my-app/0.0.1'}

>>> r = requests.get(url, headers=headers)

获取服务器响应文本内容

>>> import requests

>>> r = requests.get('https://api.github.com/events')

>>> r.text

u'[{"repository":{"open_issues":0,"url":"https://github.com/...
>>> r.encoding

'utf-8'

获取字节响应内容

>>> r.content

b'[{"repository":{"open_issues":0,"url":"https://github.com/...

获取响应码

>>> r = requests.get('https://httpbin.org/get')

>>> r.status_code

200

获取响应头

>>> r.headers

{    
   'content-encoding': 'gzip',    
   'transfer-encoding': 'chunked',  
   'connection': 'close',    
   'server': 'nginx/1.0.4',    
   'x-runtime': '148ms',    
   'etag': '"e1ca502697e5c9317743dc078f67693f"',  
   'content-type': 'application/json'
   
}

获取 Json 响应内容

>>> import requests

>>> r = requests.get('https://api.github.com/events')

>>> r.json()

[{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...

获取 socket 流响应内容

>>> r = requests.get('https://api.github.com/events', stream=True)

>>> r.raw

<urllib3.response.HTTPResponse object at 0x101194810>

>>> r.raw.read(10)

'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'

Post请求

当你想要一个键里面添加多个值的时候

>>> payload_tuples = [('key1', 'value1'), ('key1', 'value2')]

>>> r1 = requests.post('https://httpbin.org/post', data=payload_tuples)

>>> payload_dict = {'key1': ['value1', 'value2']}

>>> r2 = requests.post('https://httpbin.org/post', data=payload_dict)

>>> print(r1.text)

{  ...  "form": {    "key1": [      "value1",      "value2"    ]  },  ...}

>>> r1.text == r2.text

True

请求的时候用 json 作为参数

>>> url = 'https://api.github.com/some/endpoint'

>>> payload = {'some': 'data'}

>>> r = requests.post(url, json=payload)

想上传文件?

>>> url = 'https://httpbin.org/post'

>>> files = {'file': open('report.xls', 'rb')}

>>> r = requests.post(url, files=files)

>>> r.text

{  ...  "files": {    "file": "<censored...binary...data>"  },  ...}

获取 cookie 信息

>>> url = 'http://example.com/some/cookie/setting/url'

>>> r = requests.get(url)

>>> r.cookies['example_cookie_name']

'example_cookie_value'

发送 cookie 信息

>>> url = 'https://httpbin.org/cookies'

>>> cookies = dict(cookies_are='working')

>>> r = requests.get(url, cookies=cookies)

>>> r.text

'{"cookies": {"cookies_are": "working"}}'

设置超时

>>> requests.get('https://github.com/', timeout=0.001)

Traceback (most recent call last):
File "<stdin>", line 1, in <module>requests.exceptions.Timeout: HTTPConnectionPool(host='github.com', port=80): Request timed out. (timeout=0.001)

除了牛逼

还能说什么呢??

扫一扫

学习 Python 没烦恼

 

近期文章

python爬虫入门01:教你在Chrome浏览器轻松抓包

python爬虫入门02:教你通过Fiddler进行手机抓包

python爬虫03:那个Urllib的库让我们假装是浏览器

点好看的人

会有好运发生

最新文章

  1. Web安全相关(二):跨站请求伪造(CSRF/XSRF)
  2. [PHP][REDIS]phpredis &#39;RedisException&#39; with message &#39;read error on connection&#39;
  3. 浅谈C#中常见的委托&lt;Func,Action,Predicate&gt;(转)
  4. 大熊君大话NodeJS之------(Url,QueryString,Path)模块
  5. MAXIMO-数据库配置属性数据类型解释
  6. Sql Server对象管理器的使用
  7. fis3-postpackager-loader插件说明
  8. 2015年第5本(英文第4本):Death on the Nile尼罗河上的惨案
  9. 多个相同name的文本输入框,输入其中一个后,使剩下的不能输入值
  10. PHP创建XML文件讲解
  11. c#soap调用WebService
  12. POJ 2250 Compromise (UVA 531)
  13. CentOS6.5 常用命令
  14. SpringMVC中的@Controller和@RequestMapping到底什么鬼?
  15. Open Judge 2750 鸡兔同笼
  16. 从零开始系列之vue全家桶(4)带新手小白一起搭建第一个个人网站项目
  17. Agent Job代理 执行SSIS Package
  18. 一、activiti工作流(workflow)入门介绍
  19. flash中调用XML遇到的中文显示异常问题
  20. Rpgmakermv(24 )yep_coreengine

热门文章

  1. ExtJs--06--Ext.WindowGroup相关方法简单使用
  2. linux 启动两个tomcat
  3. Android - Error: &amp;quot;java.io.IOException: setDataSource failed.: status=0x80000000&amp;quot;
  4. jquery文件批量上传控件Uploadify3.2(java springMVC)
  5. 回调函数实现类似QT中信号机制(最简单)
  6. luogu1052 过河
  7. hdu 1874(最短路 Dilkstra +优先队列优化+spfa)
  8. k-means 聚类前的数据分析
  9. nodejs--Nodejs单元测试小结
  10. PCB MVC启动顺序与各层之间数据传递对象关系