bs4 string与text的区别
用python写爬虫时,BeautifulSoup真是解析html,快速获取所需数据的神器。
这个美味汤使唤起来,屡试不爽。
在用find()方法找到特定的tag后,想获取里面的文本,可以用.text属性或者.string属性。
在很多时候,两者的返回结果一致,但其实两者是有区别的。
.string的资料很多,.text的资料比较少。
遍寻中文世界没有满意的答案,直接google在stock overflow中找到了很满意的解答:
.string on a Tag type object returns a NavigableString type object. On the other hand, .textgets all the child strings and return concatenated using the given separator. Return type of .text is unicode object.
From the documentation, A NavigableString is just like a Python Unicode string, except that it also supports some of the features described in Navigating the tree and Searching the tree.
From the documentation on .string, we can see that, If the html is like this,
- <td>Some Table Data</td>
- <td></td>
Then, .string on the second td will return None. But .text will return and empty string which is a unicode type object.
For more convenience,
string
- Convenience property of a
tagto get the single string within this tag. - If the
taghas a single string child then the return value is that string. - If the
taghas no children or more than one child the return value isNone - If this
taghas one child tag return value is the 'string' attribute of the child tag, recursively.
And text
- Get all the child strings and return concatenated using the given separator.
If the html is like this:
- 1、<td>some text</td>
- 2、<td></td>
- 3 、<td><p>more text</p></td>
- 4、<td>even <p>more text</p></td>
.string on the four td will return,
- 1、some text
- 2、None
- 3、more text
- 4、None
.text will give result like this
- 1、some text
- 2、more text
- 3、even more text
通过以上的举例,可以很清楚的发现,.find和.string之间的差异:
第一行,在指定标签td,没有子标签,且有文本时,两者的返回结果一致,都是文本
第二行,在指定标签td,没有子标签,且没有文本时,.string返回None,.text返回为空
第三行,在指定标签td,只有一个子标签时,且文本只出现在子标签之间时,两者返回结果一致,都返回子标签内的文本
第四行,最关键的区别,在指定标签td,有子标签,并且父标签td和子标签p各自包含一段文本时,两者的返回结果,存在很大的差异
.string返回为空,因为文本数>=2,string不知道获取哪一个
.text返回的是,两段文本的拼接。
bs4 string与text的区别的更多相关文章
- "text"和new String("text")的区别
转自:What is the difference between “text” and new String(“text”)? new String("text"); expli ...
- jquery中html(), text(),val()区别(zhuan)
https://zhidao.baidu.com/question/307317838.html http://www.cnblogs.com/aqbyygyyga/archive/2011/11/0 ...
- JavaScript toString、String和stringify方法区别
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- elasticsearch的keyword与text的区别
es2.*用户可忽略该文章.es 2.*版本里面是没有这两个字段!!! 当初接触es,最惊讶就是他的版本速度发布太快,这次主要讨论keyword与text的区别 在es 2.*版本里面是没有这两个字段 ...
- String,StringBuffer,StringBuilder的区别
public static void main(String[] args) { String str = new String("hello...."); StringBuffe ...
- JQuery中的html(),text(),val()区别
jQuery中.html()用为读取和修改元素的HTML标签,.text()用来读取或修改元素的纯文本内容,.val()用来读取或修改表单元素的value值. 1.HTML html():取得第一个匹 ...
- JAVA中String与StringBuffer的区别
String和StringBuffer的区别,网上资料可以说是数不胜数,但是看到这篇文章,感觉里面做的小例子很有代表性,所以转一下,并自己做了一点总结. 在java中有3个类来负责字符的操作. 1.C ...
- String 和 StringBuffer的区别
String与StringBuffer的区别: 简单地说,就是一个常量和变量的关系.StringBuffer对象的内容可以修改:而String对象一旦产生后就不可以被修改,重新赋 ...
- [置顶] String StringBuffer StringBuilder的区别剖析
这是一道很常见的面试题目,至少我遇到过String/StringBuffer/StringBuilder的区别:String是不可变的对象(final)类型,每一次对String对象的更改均是生成一个 ...
随机推荐
- pycharm git 用法总结
一.配置git 二.登录GitHub账号 三.创建git respository 四.提交文件 五.共享给GitHub 六.修改文件push到版本库 七.从版本库checkout 项目
- 微信小程序开发demo
自己写的小程序,欢迎下载 https://gitee.com/lijunchengit/chengZiShengHuoBang
- JMeter测试HBase
在网上找了关于jmeter连接hbase的方式,主要分为两种:通过导入jar包连接(Java Request)和通过BeanShell远程连接,由于刚接触jmeter没多久,对BeanShell还不熟 ...
- drf-过滤组件|分页组件|过滤器
目录 drf-过滤组件|分页组件|过滤器 群查接口各种筛选组件数据准备 drf过滤组件 搜索过滤组件 | SearchFilter 案例: 排序过滤组件 | OrderingFilter 案例: dr ...
- Devices Tree加载流程
DT.IMG布局 hdr zImage Ramdisk.img DT.img 其中DT.img由DTBTOOL打包所有编译生成的dtb生成:布局如下: DT header dt_entry_0 dt_ ...
- Python函数Day4
一.内容补充 __iter__() 就是 iter(),iter() 调用的就是__iter__() __next__() 就是 next(),next()调用的就是__next__() __clos ...
- Go语言中的数据格式(json、xml 、msgpack、protobuf)
在分布式的系统中,因为涉及到数据的传输,所以一定会进行数据的交换,此时就要定义数据交换的格式,例如二进制.Json.Xml等等.本篇文章就是总结一下常用的几种数据格式. 一.Json格式 如果想使用J ...
- 个性化排序算法实践(一)——FM算法
因子分解机(Factorization Machine,简称FM)算法用于解决大规模稀疏数据下的特征组合问题.FM可以看做带特征交叉的LR. 理论部分可参考FM系列,通过将FM的二次项化简,其复杂度可 ...
- 【记忆化搜索】[NOIP-2017--普及组] -- 棋盘
[题目描述] 原题目链接地址: 有一个m × m的棋盘,棋盘上每一个格子可能是红色.黄色或没有任何颜色的.你现在要从棋盘的最左上角走到棋盘的最右下角. 任何一个时刻,你所站在的位置必须是有颜色的( ...
- Winform工程反编译后的工作
Winform工程,反编译后,虽然能用,但不太好用. 因为form并没有像原生的那样. 所以,需要几个步聚: 1. 用ResGen工具,把二进制资源文件还原为xml格式: ResGen fromXX. ...