最近

有些朋友

看完小帅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. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals

    感觉自己做有关区间的题目方面的思维异常的差...有时简单题都搞半天还完全没思路,,然后别人提示下立马就明白了...=_= 题意:给一个含有n个元素的数组和k,问存在多少个区间的和值为k的次方数. 题解 ...

  2. Android开发之怎样监听让Service不被杀死

    一.Service简单介绍 Service是在一段不定的时间执行在后台,不和用户交互应用组件. 每一个Service必须在manifest中 通过<service>来声明. 能够通过con ...

  3. 使用OpenCV滑动条写成的简单调色器,实时输出RGB值

    好久没有写博客了,近期在看OpenCV.于是动手写了个简单的RGB调色器,在终端实时输出RGB的值.通过这个程序学习滑动条的使用.程序中主要用到cvCreateTrackbar ,其使用方法例如以下: ...

  4. 设置用root用户telnet到linux系统

    默认情况下,ROOT用户不能以telnet方式连接Linux操作系统,而且也是不安全的.但从技术上来讲,是可以实现的. #mv /etc/securetty /etc/securetty.bak 保存 ...

  5. js简单函数封装

    //每index个字符插入一个str字符串 String.prototype.insertStrPerIndex =function(index,str){ if(this.length>ind ...

  6. inux内核模块编程入门

    linux内核模块编程入门 2013-07-06 23:59:54 分类: LINUX 原文地址:linux内核模块编程入门 作者:s270768095 模块编程属于内核编程,因此,除了对内核相关知识 ...

  7. B1076 [SCOI2008]奖励关 状压dp&&期望dp

    这个题的n<15,一看就是状压dp.但是状态不是很好想.f[][]存i关的状态j. 这个题另一个关键思想在于倒推,我一开始想的是正推,但是只能记忆化了. 题干: 题目描述 你正在玩你最喜欢的电子 ...

  8. Coursera Algorithms week3 归并排序 练习测验: Counting inversions

    题目原文: An inversion in an array a[] is a pair of entries a[i] and a[j] such that i<j but a[i]>a ...

  9. 当Shell遇上了Node.js(转载)

    转载:http://developer.51cto.com/art/201202/315066.htm 好吧,我承认,这个标题有点暧昧的基情,但是希望下文的内部能给不熟悉或不喜欢Shell或WIN平台 ...

  10. Android检测代理

    1. System.getProperties().remove("http.proxyHost"); System.getProperties().remove("ht ...