最近

有些朋友

看完小帅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. 2014秋C++ 第8周项目 分支程序设计

    课程主页在http://blog.csdn.net/sxhelijian/article/details/39152703.课程资源在云学堂"贺老师课堂"同步展示,使用的帐号请到课 ...

  2. USACO Section 2.1 Ordered Fractions

    /* ID: lucien23 PROG: frac1 LANG: C++ */ #include <iostream> #include <fstream> #include ...

  3. opencv源代码分析之二:cvhaartraining.cpp

    我使用的是opencv2.4.9.安装后.我的cvboost..cpp文件的路径是........\opencv\sources\apps\haartraining\cvhaartraining.cp ...

  4. oc63--协议@protocol1

    // // SportProtocol.h // day17 #import <Foundation/Foundation.h> @protocol SportProtocol <N ...

  5. [Contest Hunter#17-C] 舞动的夜晚

    [题目链接] http://contest-hunter.org:83/contest/CH%20Round%20%2317/%E8%88%9E%E5%8A%A8%E7%9A%84%E5%A4%9C% ...

  6. Python开发利器PyCharm 2.7附注册码

    PyCharm 2.7 下载 http://download.jetbrains.com/python/pycharm-2.7.2.exe 激活注册 user name:EMBRACE key: 14 ...

  7. 关于类和对象的进一步讨论之析构函数 C++

    析构函数也是一个特殊的成员函数.它的作用与构造函数相反.它的名字是在类名的前面加一个“~”符号.在C++中“~”是位取反运算符.当对象的生命结束时,会自动执行解析函数.以下几种情况会执行析构函数: 1 ...

  8. PCB Genesis增加点阵字 实现原理

    我们采用Genesis增加点阵字时,用Genesis增加Canned Text即可,但奥宝中文不支持,且字符种类是有限的呀 不过没关系,没有自己造呀.在这里我分享一种增加点阵字的实现方法 一.通过代码 ...

  9. [Apple开发者帐户帮助]二、管理你的团队(7)管理服务器帐户

    如果在配置机器人以在多个设备上运行应用程序时向团队添加服务器,允许Xcode Server为您管理签名,或者配置机器人以创建iOS App文件,则服务器可以访问您的资产并显示在您的开发人员帐户 您可以 ...

  10. Redis(五)-数据库

    Redis是一个字典结构的存储服务器,而实际上一个Redis实例提供了多个用来存储数据的字典,客户端可以将制定的书存储在哪个字典中,这与关系书库实例中可以i创建多个数据库类似,所以可以将其中的每个字典 ...