text和content方法的区别

2023-10-29


requests对象的get和post方法都会返回一个Response对象,这个对象里面存的是服务器返回的所有信息,包括响应头,响应状态码等。其中返回的网页部分会存在.content和.text两个对象中。

两者区别在于,content中间存的是字节码,而text中存的是Beautifulsoup根据猜测的编码方式将content内容编码成字符串。

直接输出content,会发现前面存在b'这样的标志,这是字节字符串的标志,而text是,没有前面的b,对于纯ascii码,这两个可以说一模一样,对于其他的文字,需要正确编码才能正常显示。大部分情况建议使用.text,因为显示的是汉字,但有时会显示乱码,这时需要用.content.decode('utf-8'),中文常用utf-8和GBK,GB2312等。这样可以手工选择文字编码方式。

所以简而言之,.text是现成的字符串,.content还要编码,但是.text不是所有时候显示都正常,这是就需要用.content进行手动编码。


上面的内容转自https://zhidao.baidu.com/question/941417472703558372.html


可以举几个例子看看:

import requests
from bs4 import BeautifulSoup
response = requests.get('https://www.baidu.com')
re_text = response.text
re_content = response.content
print (re_text)
print (type(re_text))
print (re_content)
print (type(re_content))
response.encoding = 'utf-8'
re_text = response.text
print (re_text)

输出结果为:(下面的分割线是手动加的)

<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');
                </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

<class 'str'>

 
 
b'<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b\xef\xbc\x8c\xe4\xbd\xa0\xe5\xb0\xb1\xe7\x9f\xa5\xe9\x81\x93</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>\xe6\x96\xb0\xe9\x97\xbb</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>\xe5\x9c\xb0\xe5\x9b\xbe</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>\xe8\xa7\x86\xe9\xa2\x91</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>\xe8\xb4\xb4\xe5\x90\xa7</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>\xe7\x99\xbb\xe5\xbd\x95</a> </noscript> <script>document.write(\'<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=\'+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ \'" name="tj_login" class="lb">\xe7\x99\xbb\xe5\xbd\x95</a>\');\r\n </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">\xe6\x9b\xb4\xe5\xa4\x9a\xe4\xba\xa7\xe5\x93\x81</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>\xe5\x85\xb3\xe4\xba\x8e\xe7\x99\xbe\xe5\xba\xa6</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>\xe4\xbd\xbf\xe7\x94\xa8\xe7\x99\xbe\xe5\xba\xa6\xe5\x89\x8d\xe5\xbf\x85\xe8\xaf\xbb</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>\xe6\x84\x8f\xe8\xa7\x81\xe5\x8f\x8d\xe9\xa6\x88</a>&nbsp;\xe4\xba\xacICP\xe8\xaf\x81030173\xe5\x8f\xb7&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>\r\n' <class 'bytes'>
<!DOCTYPE html> <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>'); </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

text和content方法的区别 的相关文章

  • 信息安全导论 实验四 RSA算法(不要求支持大数)

    一 实验目的与原理 这次次实验主要难点在于以下三个算法的理解与实现 1 Rabin Miller 算法 2 扩展欧几里得算法 3 快速幂取余算法 根据前面的算法 我们知道明文和密文都不能大于 n 假设 n 的长度为 L 对于 明文 我们需要
  • Halcon 算子 get_contour_global_attrib_xld

    作用 返回XLD轮廓的全局属性值 参数翻译 输入轮廓 输出轮廓 需要获取的属性名称 获取的属性值 描述 get contour global attrib xld返回XLD轮廓的全局属性名的值 全局属性是为每个轮廓定义的附加值 属性名 Na
  • Pytorch实现手写数字识别【基于卷积神经网络】

    说明 本案例在上一次案例的基础之上对神经网络模型进行了修改 在全连接层之前加入了特征提取层 卷积层 池化层 其他基本保持一致 卷积神经网络模型设计如下 1 导入各类需要的包 import torch import numpy as np 导
  • 3次 int 关系运算 比较两个非负 float 变量的大小

    include
  • 数据库应用:MySQL主从复制与读写分离

    目录 一 理论 1 读写分离 2 MySQL主从复制延迟原因和优化方法 3 MySQL主从复制的几个同步模式 4 MySQL读写分离 5 主从复制 异步复制 6 主从复制 半同步复制 7 读写分离 使用Amoeba 二 实验 1 主从复制
  • 帧是什么?

    数据在网络上是以很小的称为帧 Frame 的单位传输的 帧由几部分组成 不同的部分执行不同的功能 数据在网络上是以很小的称为帧 Frame 的单位传输的 帧由几部分组成 不同的部分执行不同的功能 帧通过特定的称为网络驱动程序的软件进行成型
  • 微信公众号消息与事件处理机制

    一 授权事件接收URL 1 微信服务器每隔10分钟定时推送component verify ticket 第三方平台方在收到ticket推送后也需进行解密 ComponentVerifyTicket ticket fxUU8P4 ip5B
  • sql语句group by以及count()的一些思考

    admin表 student表 第一条sql语句 SELECT admin id student name FROM admin student 结果 直接把admin id与student name匹配的所有记录 即0 1 2 3 4 5
  • 华为OD机试-MVP争夺战

    题目描述 在星球争霸篮球赛对抗赛中 强大的宇宙战队 希望每个人都能拿到MVP MVP的条件是 单场最高分得分获得者 可以并列 所以宇宙战队决定在比赛中 尽可能让更多的队员上场 且让所有有得分的队员得分都相同 然而比赛过程中的每一分钟的得分都
  • SpringCloud-消息总线

    消息总线 Spring Cloud Bus 概述 一言以蔽之 分布式自动刷新配置功能 Spring Cloud Bus配合Spring Cloud Config使用可以实现配置的动态刷新 是什么 Spring Cloud Bus 配合Spr
  • 关于 volatile——可见性,有序性,内存屏障

    并发编程的三大特性 原子性 有序性 可见性 从这三个方面去看一下 volatile volatile 保证了可见性 public class Demo1 private boolean flag true public void test
  • ​LeetCode刷题实战540:有序数组中的单一元素

    算法的重要性 我就不多说了吧 想去大厂 就必须要经过基础知识和业务逻辑面试 算法面试 所以 为了提高大家的算法能力 这个公众号后续每天带大家做一道算法题 题目就从LeetCode上面选 今天和大家聊的问题叫做 有序数组中的单一元素 我们先来
  • 模拟实现库函数strlen(详解)

    模拟实现库函数strlen 的意思是 不用strlen函数 但要求出字符串长度 1 先打印主函数 记字符串长度为len 并引入自己定义的strilen函数 含arr int main char str abcdef int len strl
  • 【python图像处理】python的图像处理模块Image【原创】

    版本信息 2 7 11 环境 windows 7 64位系统 编辑器 PyCharm 运行工具 PyCharm 文件地址 D phpStudy WWW python Image 一 引入图像模块 1 PIL简介 PIL Python Ima
  • 经典场效应管如何快速关断技巧-KIA MOS管

    mos管的快速关断原理 R4是Q1的导通电阻没有Q1就没有安装的必要了 当低电位来时Q1为泻放扩流管 功率MOS管怎样关断 能否用PWM实现 怎样实现 功率mosfet的三个端口 G极 D极 S极 G极控制mosfet的开通 关断 给GS极
  • Linux查看文件命令

    文章目录 Linux查看文件命令 1 使用vim命令 查看并编辑文件 2 使用cat命令 一次性显示文件的所有内容 3 使用head 命令 显示文件开头 默认10行 4 使用tail 命令 显示文件结尾 默认10行 vi vim工具共分为三
  • MongoDB shell操作

    MongoDB shell操作 实验目的 1 了解MongoDB的shell操作语法 2 掌握MongoDB中对数据增删改查的命令 实验原理 MongoDB shell是一个可执行文件 是MongoDB自带的一个交互式JavaScript
  • 仅需一个参数,开启微信小程序长按图片转发朋友,发送给朋友功能。小程序转发图片给朋友功能。

    原来 只需要在 image 属性加入 show menu by longpress 1 就可以实现了 太方便了吧 你是不是在这里找到答案了 给个赞呗
  • 信息收集——根据图中信息找到图中的餐厅

    这是一道网络迷踪类型的题目 先观察图片 图中比较明显的特征便是薯条和汉堡下面的小票和海报 或者类似于海报的东西 小票明显是英文的 所以初步判定该餐厅不是大陆的 去百度上搜索国外麦当劳餐厅小票 由以上三张图片可知 英文小票上对于麦当劳餐厅的描

随机推荐

  • 27道多线程核心面试题(附答案),你真的准备好面试了吗?

    今天给大家分享的是比较全面的多线程面试题 大家在面试的过程中不免会被问到很多专业性的问题 有的时候回答的并不是那么全面和精细 这仅仅代表个人观点 1 如何预防死锁 1 首先需要将死锁发生的是个必要条件讲出来 互斥条件 同一时间只能有一个线程
  • 常成员函数和常对象

    const回顾 const int a 10 a不能被改变 int const p 指针的指向可变 指向里的值不可变 int const p 指针的指向不可变 指向里的值可变 int const const p 都不可变 const int
  • PageHelper分页插件使用

    http blog csdn net nz360 article details 52326232 主要记录自己使用PageHelper分页的过程 需要的jar包 java view plain copy
  • 关于Context(上下文)的理解

    一直听到上下文一说 一直没弄清楚到底是啥意思 今天总结一下 不知道对不对 感觉对Context这个词翻译的不太好 不应该叫上下文 应该直接就叫 环境 不过都这么叫 就叫上下文好了 所谓的上下文就是指语境 每一段程序都有很多的外部变量 只有想
  • Python四种常用的高阶函数,你会用了吗

    1 什么是高阶函数 把函数作为参数传入 这样的函数称为高阶函数 例如 def func1 x y f return f x f y num func1 10 2 abs print num 注意 把函数作为参数传入的时候 不要再传入函数后面
  • Latex中cases环境引入报错

    编写 begin equation C i begin cases 0 i 0 max 0 x i bar x C i 1 i neq 0 end cases end equation 时报错 需要导入宏包 usepackage amsma
  • 利用云主机搭建游戏加速器(仅限国内加速)

    利用云主机搭建游戏加速器 仅限用于中国大陆内游戏加速服务 注意 仅限用于中国大陆内游戏加速服务 禁止开飞机 最近在玩西山居公司的剑网三游戏 因为自己用的是联通宽带 但是玩的是电信区的服务器 我使用的是腾讯云的云主机 大家可以根据需要自己来做
  • BES2300x笔记(20) -- IIC驱动

    哈喽大家好 这是该系列博文的第二十篇 篇 lt lt 系列博文索引 快速通道 gt gt 一 前言 说到IIC驱动 绝对是一个老生常谈的话题 但凡入坑了嵌入式开发的道友 肯定都能说的头头是道 编写个驱动 调试个外设 那更是信手拈来 不在话下
  • java 静态分析工具_了解您的Java静态分析工具

    java 静态分析工具 有许多Java静态分析工具在起作用 每个人都专注于特定领域并具有自己独特的优势 这是一个有用的摘要 Pmd 基于静态规则集的Java源代码分析器 它识别潜在的问题 例如 可能的错误 尝试 捕获 最终 切换块为空 无效
  • 解析 ETH 区块数据交易input

    这里使用了一个开源项目 https github com rvullriede evm abi decoder 在pom中添加依赖即可
  • Pytorch中nn.LSTM与nn.LSTMCell

    class torch nn LSTM args kwargs 对输入序列的每个元素 LSTM的每层都会执行以下计算 h t h t ht 是时刻 t t t的隐状态 c t c t ct 是时刻 t t t的细胞状态 x t x t xt
  • CURL命令 : GET、POST请求、文件下载等常用命令

    文章目录 1 curl介绍 2 直接发送请求 Get 3 发送Post请求 3 1 Post请求带参数 4 Put请求和Delete请求 5 携带首部信息 6 获取响应的所有的首部信息 7 文件下载 8 显示底层连接信息 9 通过代理访问
  • C++的头文件和实现文件分别写什么

    文章来源 http www cnblogs com ider archive 2011 06 30 what is in cpp header and implementation file html 在C 编程过程中 随着项目的越来越大
  • javaweb出现HTTP500的可能问题的解决方案

    我的解决方案是在webContent WEb INf lib下面添加mysql的jar包即可 如下图 加上后如下图 亲测有效
  • 做一个合格程序员的7条定律

    一 低承诺 高实现 如果你的承诺确实是管理人员想听到的 他就会喜欢你 但是 如果不能按照承诺及时交付软件 他就不会再喜欢你 二 不要在软件中放入错误 优秀的程序员不在他们的代码中放入错误 三 充满热情 努力工作 优秀的程序员是充满热情和努力
  • python中xlrd.open_workbook_python处理exce方法:xlrd、openyxl

    excel 2010及以上 xlsx 使用 openpyxl excel 2007 xls 使用xlrd pandas也是依赖xlrd openpyxl xlrd import xlrd data xlrd open workbook r
  • R手册(Import)--rvest

    文章目录 解析html 提取组件 提取 修改和提交形式的函数 浏览网站 解析html 函数 说明 read html x encoding x为a url或 a local path html nodes x css xpath 通过使用
  • C++ Visual Studio报错:error LNKXXX: 无法解析的外部符号 __imp_XXXXX的解决方法

    C Visual Studio报错 error LNKXXX 无法解析的外部符号 imp XXXXX的解决方法 我们复制一项报错中 imp 后面的字符 如报错第一行是 TranslateMessage 我们把它粘贴至浏览中搜索 找到micr
  • 深度强化学习:PPO + AI2THOR问题总结

    一 PPO代码 选用的是如下repo中的PPO代码 https github com MorvanZhou Reinforcement learning with tensorflow tree master contents 二 网络结构
  • text和content方法的区别

    requests对象的get和post方法都会返回一个Response对象 这个对象里面存的是服务器返回的所有信息 包括响应头 响应状态码等 其中返回的网页部分会存在 content和 text两个对象中 两者区别在于 content中间存