添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
英俊的猴子  ·  Linux 中C/C++ search ...·  7 月前    · 
冷冷的电梯  ·  javascript - js ...·  1 年前    · 
成熟的橡皮擦  ·  Can't import UDF from ...·  2 年前    · 
import requests
response=requests.get("http://www.baidu.com/")
print(response)            #  <Response [200]>
print(type(response))   # <class 'requests.models.Response'>
1. response.status_code

http请求的返回状态,2XX 表示连接成功,3XX 表示跳转 , 4XX 客户端错误 , 500 服务器错误

2. response.text

http响应内容的 字符串(str) 形式,请求url对应的页面内容

response=requests.get("http://www.baidu.com/")
print(response.text)

打印出的内容含有乱码:

# <title>ç™¾åº¦ä¸€ä¸‹ï¼Œä½ å°±çŸ¥é“</title>

修改如下, 改变下载得到的页面的编码,就可以正常打印出"友好的"文本了:

response.encoding="utf-8"
print(response.text)  # 打印文本中没有乱码

小结 : 更改编码使用:response.encoding=“utf-8” 或者 response.encoding=”gbk”
具体要看你请求的网页是用什么方式编码的,针对不同情况用对应的编码方式.

比如下面这个例子, 不用编码也可以打印正常文本 ,如果你还是用response.encoding=“utf-8” ,反而会出现乱码

# 没有乱码:
response =requests.get("http://www.qq.com/")
print(response.text)   
response =requests.get("http://www.qq.com/")
response.encoding="gbk"  
print(response.text)    
# 有乱码:
response =requests.get("http://www.qq.com/")
response.encoding="utf-8"  
print(response.text)    
3. response.content

HTTP响应内容的 二进制(bytes) 形式

response =requests.get("http://www.baidu.com/")
# print(response.content)     #打印出的是二进制形式
print(response.content.decode("utf-8"))
response =requests.get("http://www.qq.com/")
# print(response.content)     #打印出的是二进制形式 
print(response.content.decode("gbk"))

小结:更改编码使用 response.content.deocde(“utf8”)
更推荐使用response.content.deocde()的方式获取响应的html页面.

4. response.encoding

从HTTP header中猜测的响应内容编码方式

5. response.apparent_encoding

从内容分析出的响应内容的编码方式(备选编码方式)

6. response.headers

http响应内容的头部内容

import requests, json r = requests.get('http://192.168.207.160:9000/api/qualitygates/project_status?projectId=%s' % (p_uuid) ) state=json.loads(r.text).get('projectStatus').get('status') 返回如下: "projectStatus": { "status": "ERROR", "conditions": [{ "status": "ERROR", 想要学习PythonPython学习交流群:973783996满足你的需求,资料都已经上传群文件,可以自行下载! response = requests.get(https://www.baidu.com/) #text返回的是unicode的字符串,可能会出现乱码情况 # print(response.text) #content返回的是字节,需要解码 print(response.content.decode('utf-8')) # print(response.url) #https://www.b 定义在类的变量称为属性。定义在类的函数称为方法。属性是类的特征,描述了类的状态。方法是类的行为,描述了类的操作。属性和方法都是类的成员,可以被类的实例访问和使用。在面向对象编程,类是一个模板,实例是根据模板创建的对象。每个实例都有自己的属性和方法,但是它们的定义都是基于类的。属性和方法可以是公共的、私有的或受保护的。公共的属性和方法可以被任何人访问和使用,私有的属性和方法只能在类内部访问和使用,受保护的属性和方法可以在类内部和子类访问和使用。【总结】属性 = 变量。 def text(self): """Content of the response, in unicode. If Response.encoding is None, encoding will be guessed using ``chardet``. The encoding of the response content is determined based solely on HTTP 1. response.content这个是直接从网络上面抓取的数据,没有经过任何解码,所以是一个 bytes类型,其实在硬盘上和在网络上传输的字符串都是 bytes类型, 2. response,text:这个是 requests,将 response.content进行解码的字符串,解码需要指定一个编码方式, requests会根据自己的猜测来判断编码的方式,所以有时候可能会猜测错误,就会导致... get请求简单使用import requests#Python学习交流群:973783996response = requests.get("https://www.baidu.com/")#text返回的是unicode的字符串,可能会出现乱码情况# print(response.text)#content返回的是字节,需要解码print(response.content.decode('utf... 在某些情况下来说,response.textresponse.content 都是来获取response的数据信息,效果看起来差不多。那么response.textresponse.content 到底有哪些差别 ? 什么情况下该用 response.text 什么情况下该用 response.content ? 返回的数据类型 response.text 返回的是一个 uni... response.text() 功能:Sanic 返回纯文本内容给浏览器。作为一个完整功能的web网站,一般是不会返回纯文本内容的,特殊情况下可选择使用本函数。response.text() 语法def text(body,status=200, headers=None,content_type="text/plain;charset=utf-8"):response.text() 参数body... Response.text:文本格式输出的返回报文体 Response.json():json格式返回的报文体,字典类型 Response.status_code:服务器响应的状态码 Response.headers:服务器响应报文头,大小写不敏感的字典类型