发送请求

使用Requests发送网络请求很简单

#首先要导入requests库
import requests
#返回一个Response对象
r=requests.get('https://github.com/timeline.json')
#由此可以看出来Requests的API十分简单, #post请求
r=requests.post('http://httpbin.org/post')
#Put请求,delete请求,head请求,options请求
r=requests.put('http://httpbin.org/put')

传递URL参数

一般的get传递参数方法是将数据与url地址用?连起来。

Requests库允许使用params关键字参数,以一个dict来提供这些参数。

import requests
payload={'key1':'value1','key2':'value2'}
r=requests.get('http://httpbin.org/get',params=payload)
print(r.url)#可以看出response对象由url属性
http://httpbin.org/get?key1=value1&key2=value2

响应内容

通过Response的text属性,我们可以读取服务器响应的内容。并且Requests会自动解码来自服务器的内容。

import requests
r=requests.get('https://github.com/timeline.json')
r.text#已经经过自动解码
'{"message":"Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.","documentation_url":"https://developer.github.com/v3/activity/events/#list-public-events"}'
r.encoding
#通过encoding属性显示响应的编码,并且可以改变编码属性。可以使用自定义的编码
'utf-8'

二进制响应内容

以字节的方式访问请求响应体

r.content#未解码的内容,
b'{"message":"Hello there, wayfaring stranger. If you\xe2\x80\x99re reading this then you probably didn\xe2\x80\x99t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.","documentation_url":"https://developer.github.com/v3/activity/events/#list-public-events"}'
#Requests会自动解码gzip和deflate传输编码的响应数据。
#举例,以请求返回的二进制数据创建一张图片
from PIL import Image
from io import BytesIO i=Image.open(BytesIO(r.content))

JSON响应内容

import requests
r=requests.get('https://github.com/timeline.json')
r.json()
{'documentation_url': 'https://developer.github.com/v3/activity/events/#list-public-events',
'message': 'Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.'}

定制请求头headers

所有的header必须是string、bytestring或者unicode

url='https://api.github.com/some/endpoint'
headers={'user-agent':'my-app/0.0.1'} r=requests.get(url,headers=headers)

复杂的POST请求

payload={'key1':'value1','key2':'value2'}
#这里还可以讲dict替换成元祖列表,尤其在表单中多个元素使用同一key的时候。
#还可以将data=替换成json=,传入json对象。
r=requests.post('http://httpbin.org/post',data=payload)
print(r.text)
{
"args": {},
"data": "",
"files": {},
"form": {
"key1": "value1",
"key2": "value2"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Content-Length": "23",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.4"
},
"json": null,
"origin": "36.102.236.202",
"url": "http://httpbin.org/post"
}

POST一个多部分编码的文件

url='http://httpbin.org/post'
files={'file':open('report.xls','rb')}#强烈建议使用二进制模式打开文件。
r=requests.post(url,files=files)
r.text#因为我们没有report.xls文件,所以不展示输出结果了

响应状态码

r.status_code#获取状态码
r.status_code==requests.codes.ok#内置由状态码查询对象
#如果发送了一个错误请求,可以通过Response.raise_for_status()抛出异常,如果不是错误码,抛出None

响应头

r.headers
{'Date': 'Thu, 16 Nov 2017 13:01:18 GMT', 'Content-Type': 'application/json', 'Access-Control-Allow-Credentials': 'true', 'X-Processed-Time': '0.00141000747681', 'Via': '1.1 vegur', 'Content-Length': '465', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Server': 'meinheld/0.6.1', 'X-Powered-By': 'Flask'}

Cookie

#访问cookie
r.cookies['cookie_name']
#发送cookie到服务器
cookies=dict(cookies_are='workding')
r.requests.get(url,cookies=cookies)
r.text#会打印出cookie来 #Cookie的返回对象为RequestsCookieJar,类似于字典,适合跨域名路径使用。
jar=requests.cookies.RequestsCookieJar()
jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
jar.set('gross_cookie', 'blech', domain='httpbin.org', path='/elsewhere')
url = 'http://httpbin.org/cookies'
r = requests.get(url, cookies=jar)
r.text

重定向与请求历史

除了HEAD,Requests会自动处理所有重定向。

可以使用history方法来追踪。

超时

设置timeout参数,以秒为单位。

timeout仅对连接过程有效,与响应体的下载无关。

会话对象

Session对象能够实现跨请求保持参数。并且在同一个session实例发出的所有请求之间保持cookie。

Session对象具有主要的RequestsAPI的所有方法。

s=requests.Session()
s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
r=s.get('http://httpbin.org/cookies') print(r.text)
{
"cookies": {
"sessioncookie": "123456789"
}
}
#会话也可用来为请求方法提供缺省数据
s=request.Session()
s.auth=('user','pass')
s.headers.update({'x-test':'true'})
#x-test和x-test2都会发送出去
s.get('http://httpbin.org/headers',headers={'x-test2':'true'})
#任何传递给请求方法的字典都会与已设置会话层数据合并。但是方法级别的参数不会被跨请求保持,比如:
s=requests.Session()
r=s.get('http://httpbin.org/cookies',cookies={'from-my':'browser'})
print(r.text)
{
"cookies": {
"from-my": "browser"
}
}
r=s.get('http://httpbin.org/cookies')
print(r.text)
{
"cookies": {}
}

请求与响应对象

其实requests.get()调用,第一是构建一个Request对象,向某个服务器发送请求,第二是从服务器返回的响应Response对象,该对象包含服务器返回的所有信息。

r.headers#访问服务器返回来的响应头部信息
r.request.headers#获取发送到服务器的请求的头部信息

准备的请求

如果在发送请求之前,需要对body或者header进行额外处理,可以这么做:

from requests import Request,Session

s=Session()
req=Request('GET',url,data=data,headers=header)
prepped=req.prepare()
#对prepped的body和header进行修改
resp=s.send(prepped,stream=stream,verify=verify,proxies=proxies,cert=cert,timeout=timeout)
print(resp.status_code)

但是上述代码会失去Requests Session对象的一些优势,尤其是Session级别的状态,要获取一个带有状态的PreparedRequest,需要用Session.prepare_request()取代Request.prepare()的调用。

SSL证书验证

SSL验证默认开始,如果验证失败,会抛出SSLError

requests.get('http://requestb.in')
<Response [200]>
requests.get('https://github.com',verify=True)
#如果设置为False,会忽略对SSL证书的验证。
<Response [200]>

verify可以是包含可信任CA证书的文件夹路径:比如verify='/path/to/certfile'。

或者将verify保持在会话中:

s = requests.Session()
s.verify = '/path/to/certfile'

代理-proxies参数

import requests

proxies = {
"http": "http://10.10.1.10:3128",
"https": "http://10.10.1.10:1080",
} requests.get("http://example.org", proxies=proxies)

如果您觉得感兴趣的话,可以添加我的微信公众号:一步一步学Python

爬虫入门【2】Requests库简介的更多相关文章

  1. 网络爬虫入门:你的第一个爬虫项目(requests库)

    0.采用requests库 虽然urllib库应用也很广泛,而且作为Python自带的库无需安装,但是大部分的现在python爬虫都应用requests库来处理复杂的http请求.requests库语 ...

  2. 爬虫入门之urllib库详解(二)

    爬虫入门之urllib库详解(二) 1 urllib模块 urllib模块是一个运用于URL的包 urllib.request用于访问和读取URLS urllib.error包括了所有urllib.r ...

  3. Python爬虫入门——使用requests爬取python岗位招聘数据

    爬虫目的 使用requests库和BeautifulSoup4库来爬取拉勾网Python相关岗位数据 爬虫工具 使用Requests库发送http请求,然后用BeautifulSoup库解析HTML文 ...

  4. 从0开始学爬虫9之requests库的学习之环境搭建

    从0开始学爬虫9之requests库的学习之环境搭建 Requests库的环境搭建 环境:python2.7.9版本 参考文档:http://2.python-requests.org/zh_CN/l ...

  5. 芝麻HTTP: Python爬虫利器之Requests库的用法

    前言 之前我们用了 urllib 库,这个作为入门的工具还是不错的,对了解一些爬虫的基本理念,掌握爬虫爬取的流程有所帮助.入门之后,我们就需要学习一些更加高级的内容和工具来方便我们的爬取.那么这一节来 ...

  6. Python爬虫入门之Urllib库的高级用法

    1.设置Headers 有些网站不会同意程序直接用上面的方式进行访问,如果识别有问题,那么站点根本不会响应,所以为了完全模拟浏览器的工作,我们需要设置一些Headers 的属性. 首先,打开我们的浏览 ...

  7. Python爬虫入门之Urllib库的基本使用

    那么接下来,小伙伴们就一起和我真正迈向我们的爬虫之路吧. 1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由浏览器解 ...

  8. python爬虫之一:requests库

    目录 安装requtests requests库的连接异常 HTTP协议 HTTP协议对资源的操作 requests库的7个主要方法 request方法 get方法 网络爬虫引发的问题 robots协 ...

  9. PYTHON 爬虫笔记三:Requests库的基本使用

    知识点一:Requests的详解及其基本使用方法 什么是requests库 Requests库是用Python编写的,基于urllib,采用Apache2 Licensed开源协议的HTTP库,相比u ...

随机推荐

  1. 在elasticsearch里如何高效的使用filter

    今天在做查询category的时候,遇到一个问题,查询出来的cateogry为food,fun的形式.但是我需要的只是food或者fun 不包含逗号. 开始想着在aggs后再做过滤,这样有些麻烦.遂在 ...

  2. 【Hadoop】Flink VS Spark?Drill VS Presto?

    参考资料: drill 官网:http://drill.apache.org/ drill安装使用:https://segmentfault.com/a/1190000002652348 drill简 ...

  3. zabbix监控php-fpm

    1.启用php-fpm的状态功能 [root@web01 ~]# vim /etc/php-fpm.d/www.conf 121 pm.status_path = /php_status [root@ ...

  4. 搭建elsticsearch集群 报错with the same id but is a different node instance解决办法

    搭建elsticsearch集群 报错with the same id but is a different node instance解决办法 学习了:https://blog.csdn.net/q ...

  5. EffectiveJava(9)覆盖equals是总要覆盖hashCode

    覆盖equals是总要覆盖hashCode 通过散列函数将集合中不相等的实例均匀的分布在所有可能的散列值上 1.把某个非零的常数值保存在一个名为result的int类型变量中 2.对于对象中每个关键域 ...

  6. 内网IPC$种马的三种方法

    copy muma.exe \\host\c$\windows\temp\foobar.exe ##IPC拷贝木马文件 WMIC远程运行命令 wmic /node:host /user:adminis ...

  7. ionic准备之angular基础——run方法(4)

    可以看到整个angular.module对象具有以下各种属性和方法 <!DOCTYPE html> <html lang="en"> <head> ...

  8. C# Socket.Connect连接请求超时机制

    介绍 您可能注意到了,.Net的System.Net.Sockets.TcpClient和System.Net.Sockets.Socket都没有直接为Connect/BeginConnect提供超时 ...

  9. 2015级C++第7周项目 友元、共享数据保护、多文件结构

    [项目1-成员函数.友元函数和一般函数有差别]參考解答 (1)阅读以下的程序,体会凝视中的说明(要执行程序,请找到课程主页并复制代码) //例:使用成员函数.友元函数和一般函数的差别 #include ...

  10. 【BIEE】11_根据显示指标展示不同报表

    报表开发过程中,我们经常会需要根据所选择的不同指标,展示不同的报表!例如: 显示指标有:金额与合同数,可以根据显示指标选择的内容进行相应报表数据展示 一.环境准备 create table crm_i ...