最近

有些朋友

看完小帅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的库让我们假装是浏览器

点好看的人

会有好运发生

python爬虫04 | 长江后浪推前浪,Reuqests库把urllib库拍在沙滩上的更多相关文章

  1. Python爬虫从入门到进阶(2)之urllib库的使用

    1.什么是Urllib(官网地址:https://docs.python.org/3/library/urllib.html#module-urllib) Urllib是python内置的HTTP请求 ...

  2. $python爬虫系列(2)—— requests和BeautifulSoup库的基本用法

    本文主要介绍python爬虫的两大利器:requests和BeautifulSoup库的基本用法. 1. 安装requests和BeautifulSoup库 可以通过3种方式安装: easy_inst ...

  3. Python爬虫笔记一(来自MOOC) Requests库入门

    Python爬虫笔记一(来自MOOC) 提示:本文是我在中国大学MOOC里面自学以及敲的一部分代码,纯一个记录文,如果刚好有人也是看的这个课,方便搬运在自己电脑上运行. 课程为:北京理工大学-嵩天-P ...

  4. 《爬虫学习》(二)(urllib库使用)

    urllib库是Python中一个最基本的网络请求库.可以模拟浏览器的行为,向指定的服务器发送一个请求,并可以保存服务器返回的数据. 1.urlopen函数: 在Python3的urllib库中,所有 ...

  5. [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)

    转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...

  6. 爬虫笔记(一)——快速使用urllib库

    本人以前用的都是python2.7,但看网上很多教程都是以python3为例的,所以便切换版本,导入urllib.lxml.beautifulsoup4等库. 下面介绍下两个版本对urllib库的区别 ...

  7. Python爬虫利器三之Xpath语法与lxml库的用法

    前面我们介绍了 BeautifulSoup 的用法,这个已经是非常强大的库了,不过还有一些比较流行的解析库,例如 lxml,使用的是 Xpath 语法,同样是效率比较高的解析方法.如果大家对 Beau ...

  8. 【Python爬虫学习笔记(1)】urllib2库相关知识点总结

    1. urllib2的opener和handler概念 1.1 Openers: 当你获取一个URL你使用一个opener(一个urllib2.OpenerDirector的实例).正常情况下,我们使 ...

  9. Python爬虫-04:贴吧爬虫以及GET和POST的区别

    目录 1. URL的组成 2. 贴吧爬虫 2.1. 只爬贴吧第一页 2.2. 爬取所有贴吧的页面 3. GET和POST的区别 3.1. GET请求 3.2. POST请求 3.3. 有道翻译模拟发送 ...

随机推荐

  1. 【Git使用具体解释】Egit使用过程中遇到的问题及解决的方法

    1.   Git错误non-fast-forward后的冲突解决 问题(Non-fast-forward)的出现原因在于:git仓库中已经有一部分代码,所以它不同意你直接把你的代码覆盖上去.于是你有2 ...

  2. bootstrap异步加载树后样式显示问题

    整个过程: 1.先加载整个页面 2.通过jquery异步请求后台返回数据 3.循环遍历数据,拼接需要的内容 4.把拼接好的数据加载到页面中. 问题: 把拼接好的内容加载到页面后,样式显示不正确.而如果 ...

  3. Elias-Fano编码算法——倒排索引压缩用,本质上就是桶排序数据结构思路

    Elias-Fano编码过程如下:把一组整数的最低l位连接在一起,同时把高位以严格单调增的排序划分为桶. Example: 2, 3, 5, 7, 11, 13, 24 Count in unary ...

  4. python lmdb demo 这接口和BDB一样恶心啊!

    import lmdb lmdb_img_name = "test.lmdb" env = lmdb.open(lmdb_img_name, map_size=1e6) with ...

  5. C++_homework_EraseComment

    顾名思义就是删除程序中的注释,不清楚fsm的机制,完全是自己的思路做. 开头先读取一个字符确定是否到文件结尾,如果读取成功,是换行的话就换行,并继续读取,不是的话,用putback放回缓冲区,并整行读 ...

  6. 服务器通信REST、gRPC,Swagger/OpenAPI

    服务间的通信方式是在采用微服务架构时需要做出一个最基本的决策.默认的选项是通过 HTTP 发送 JSON,也就是所谓的 REST API.我们也是从 REST 开始的,但最近我们决定改用 gRPC. ...

  7. JPA实体关联关系,一对一以及转换器

    现有两张表 room (rid,name,address,floor) room_detail (rid,roomid,type) 需要创建房间实体,但是也要包含type属性 @Data //lamb ...

  8. 如何在linux下搭建svn服务

    • 安装svn 使用命令 yum install subversion 如果提示上述错误,请以管理员身份运行 使用命令su root 再执行 yum install subversion 2,查看sv ...

  9. C - Between the Offices

    Problem description As you may know, MemSQL has American offices in both San Francisco and Seattle. ...

  10. MyEclipse中快速复制粘贴当前行的操作