首页 > 其他分享 >requests模块Response类

requests模块Response类

时间:2023-04-16 20:22:56浏览次数:25  
标签:redirect encoding res apparent 模块 print requests type Response

1、介绍

requests.request函数执行的返回对象即requests.Response类的对象。当然,其他请求函数执行返回也是如此。

2、类和初始化

class Response:
    __attrs__ = [
        "_content",
        "status_code",
        "headers",
        "url",
        "history",
        "encoding",
        "reason",
        "cookies",
        "elapsed",
        "request",
    ]
    def __init__(self):
  • Response类不支持外部创建Response对象初始化

3、属性和方法

(1)响应

print(res, type(res))
# <Response [302]> <class 'requests.models.Response'>
  • 请求执行之后会返回一个响应对象

(2)headers

作用:获取响应的头部字段信息

其类似dict类型,可用通过索引取值,或者str函数转换,或者调用dict类型的方法

如果取值没有该字段,会报错

print(res.headers, type(res.headers))
# {'Server': 'nginx/1.15.5', 'Date': 'Sat, 21 Aug 2021 21:10:41 GMT', 'Content-Type': 'text/html', 'Content-Length': '157', 'Connection': 'keep-alive'} <class 'requests.structures.CaseInsensitiveDict'>
print(res.headers['Server'])
print(res.headers.values())

(3)url、raw、ok、is_redirect、reason、status_code

print(res.url, type(res.url))
print(res.raw, type(res.raw))
print(res.ok, type(res.ok))
print(res.is_redirect, type(res.is_redirect))
print(res.reason, type(res.reason))
print(res.status_code, type(res.status_code))
'''
https://www.baidu.com/ <class 'str'>
<urllib3.response.HTTPResponse object at 0x0000018470426AF0> <class 'urllib3.response.HTTPResponse'>
True <class 'bool'>
False <class 'bool'>
OK <class 'str'>
200 <class 'int'>
'''
  • url:str类型
  • raw:暂时未找到应用方式
  • ok:bool类型,判断是否为200
  • is_redirect:bool类型,判断是否为3xx
  • reason:str类型,状态描述
  • status_code:int类型,状态代码

(4)encoding、apparent_encoding、cookies、history、

print(res.encoding, type(res.encoding))
print(res.apparent_encoding, type(res.apparent_encoding))
print(res.cookies, type(res.cookies))
print(res.history, type(res.history))
'''
ISO-8859-1 <class 'str'>
utf-8 <class 'str'>
<RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]> <class 'requests.cookies.RequestsCookieJar'>
[] <class 'list'>
'''
  • encoding:编码,str类型
  • apparent_encoding:表现编码,str类型
  • cookies:自定义类型,可以通过其对象方法操作
  • history:历史,list类型

(5)content、text、links

print(res.text.encode(encoding=res.encoding).decode(res.apparent_encoding))
print(res.content, type(res.content))
print(res.links, type(res.links))
'''
xxx
b'xxx' <class 'bytes'>
{} <class 'dict'>
'''
  • text:内容部分,str类型。其一般会进行字符编码传输,res.encoding指定传输编码。而apparent_encoding指定重新编码
    • 但注意res.encoding参数可能会为None,该请求会报错,所以需要先做判断
    • apparent_encoding一般直接指定为utf-8
  • context:内容部分,bytes字节类型
  • links:字典类型

(6)elapsed、next、is_permanent_redirect

print(res.elapsed, type(res.elapsed))
print(res.next, type(res.next))
print(res.is_permanent_redirect, type(res.is_permanent_redirect))
'''
0:00:00.230697 <class 'datetime.timedelta'>
None <class 'NoneType'>
False <class 'bool'>
'''
  • elapsed:请求的响应时间
  • next:一般是重定向使用的路径
  • is_permanent_redirect:是否永久重定向

标签:redirect,encoding,res,apparent,模块,print,requests,type,Response
From: https://www.cnblogs.com/wd404/p/17323964.html

相关文章

  • 汉源高科1分8口工业级485集线器8路16路中继器分线器485hub信号隔离器模块
    汉源高科1分8口工业级485集线器8路16路中继器分线器485hub信号隔离器模块......
  • Nginx之数据流代理stream模块简介和使用
    转自 http://t.csdn.cn/RV4Hi一、stream模块简介  stream模块一般用于TCP/UDP数据流的代理和负载均衡,通过stream模块我们可以代理转发tcp报文。ngx_stream_core_module模块从1.9.0版开始提供。默认情况下,此模块不是构建的,应该使用–withstream配置参数启用它,即我们需要使用.......
  • burp攻击模块
    1、介绍intruder入侵模块,实际上是基于不同策略的重复请求,然后测试人员可以结合测试参数,分析响应。从其他模块,选择请求,右键选择sendtointruder,可以将其请求提交到intruder模块进行操作。2、攻击类型选择和测试文本标记(1)攻击类型有四种sniper:狙击手模式,使用一个payload集合......
  • abp(net core)+easyui+efcore实现仓储管理系统——模块管理升级之上(六十一)
     Abp(netcore)+easyui+efcore实现仓储管理系统目录abp(netcore)+easyui+efcore实现仓储管理系统——ABP总体介绍(一)abp(netcore)+easyui+efcore实现仓储管理系统——解决方案介绍(二)abp(netcore)+easyui+efcore实现仓储管理系统——领域层创建实体(三) abp(netcore)+e......
  • 七彩RGB可控模块教程
    一、硬件介绍七彩RGB可控模块,是个LED灯。但是它有三种颜色,分别为红、绿、蓝。该模块有四个接口,分别是Gnd、R、G、B。 二、控制原理通过PWM来控制LED灯的亮度,除此之外R、G、B、三个口要接三个不同的GPIO口。其实你也将RGB可控模块理解为三个不同颜色的LED灯。 三、......
  • Python模块-requests
    1、requests模块requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求。功能强大,用法简洁高效。2、模块介绍及请求过程requests模块模拟浏览器发送请求请求流程:指定url-->发起请求-->获取响应对象中存储的数据-->持久化存储3、爬取百度首页#!......
  • Python模块-socket
    1、基于TCP协议的socket通信以打电话为理解方式进行TCP的通信#Server端importsocketphone=socket.socket(socket.AF_INET,socket.SOCK_STREAM)#购买电话卡,AF_INET服务器之间网络通信,socket.SOCK_STREAM,流式协议,就是TCP协议phone.bind(('127.0.0.1',8080))......
  • java——maven——分模块——资源加载属性值
    第一步:   第二步:    第三步:                       ......
  • java——maven——分模块——属性定义与使用
                   版本号统一管理                 ......
  • java——maven——分模块——模块继承
    通过父工程,管理所有子模块的依赖版本管理    把所有依赖放入dependentmanagement下面        所有的子工程需要修改,引入父工程,然后子工程里面的引入依赖的版本号全部去除,交由父工程统一管理:       插件依赖,也可以进行版本统一管理:......