python爬虫04 | 长江后浪推前浪,Reuqests库把urllib库拍在沙滩上

最近

 

有些朋友

 

看完小帅b的文章之后

 

把小帅b的表情包都偷了

 

还在我的微信

 

疯狂发表情包嘚瑟

 

我就呵呵了

 

640?wx_fmt.jpeg

 

只能说一句

 

盘他

 

还有一些朋友

 

看完文章不点好看

 

还来催更

 

小帅b也只能说一句

 

继续盘他

 

640?wx_fmt.jpeg

 

ok

 

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

 

这个库的名称叫做

 

Requests

 

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

 

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

 

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

 

模拟浏览器操作

 

人生苦短

 

接下来就是

 

学习 python 的正确姿势

 

 

640?wx_fmt.jpeg

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)

 

640?wx_fmt.jpeg

 

除了牛逼

 

还能说什么呢??

 

 

640?wx_fmt.gif

扫一扫

学习 Python 没烦恼

 

640.jpg

 

近期文章

 

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

 

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

 

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

 

 

 

 

点赞的人

会有好运发生

转载于:https://my.oschina.net/u/3887904/blog/3047071

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值