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对象的更改均是生成一个 ...
随机推荐
- 记录java+testng运行selenium(四)--- 运行代码
涉及的文件有: .\medical\BusinessFile.java :实例化excel及xml文件操作对象以及将list变成Map .\medical\manual\business\LoginB ...
- PAT基础级-钻石段位样卷2-7-2 吃鱼还是吃肉 (10 分)
国家给出了 8 岁男宝宝的标准身高为 130 厘米.标准体重为 27 公斤:8 岁女宝宝的标准身高为 129 厘米.标准体重为 25 公斤. 现在你要根据小宝宝的身高体重,给出补充营养的建议. 输 ...
- Alpha版本第二周小结
软工作业---Alpha版本第二周小结 姓名 学号 周前计划 每周实际工作记录 自我打分 yrz 1417 协助原型设计的完善,督促组员完成个人任务 原型优化设计未完成,但体 ...
- 跨站请求伪造(CSRF 或者 XSRF)与跨站脚本(XSS)
跨站请求伪造 跨站请求伪造(英语:Cross-site request forgery),也被称为 one-click attack 或者 session riding,通常缩写为 CSRF 或者 X ...
- js 数组中如何删除字典
区别: []表示是一个数组,如var strs = ['a','b','c'].{}表示是一个对象,比如,var obj = {name: '宙斯',sex: 1} 如何在数组中删除指定对象呢?? [ ...
- Laravel $request 常用方法
request的方法 描述 input('key','default-value') 此方法可以获取get请求所带来的特定参数的值,如果没有该参数可以提供默认值 all() 此方法获取get请求的所有 ...
- POI之SXSSFWorkbook大量数据导出至excel
一:简介 SXSSFWorkbook是用来生成海量excel数据文件,主要原理是借助临时存储空间生成excel, SXSSFWorkbook专门处理大数据,对于大型 ...
- 洛谷 P1309 瑞士轮 题解
每日一题 day4 打卡 Analysis 暴力+快排(其实是归并排序) 一开始天真的以为sort能过,结果光荣TLE,由于每次只更改相邻的元素,于是善于处理随机数的快排就会浪费很多时间.于是就想到归 ...
- 重载new和delete运算符
内存管理运算符 new.new[].delete 和 delete[] 也可以进行重载,其重载形式既可以是类的成员函数,也可以是全局函数.一般情况下,内建的内存管理运算符就够用了,只有在需要自己管理内 ...
- 好用的Google Chrome插件
juejin掘金 帮助我们随时了解Github上热门的项目,保证技术思想不掉队. Momentum 专注.待办清单…… Octotree Octotree 可以让我们在 Github 上浏览代码更加方 ...