用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,

  1.  
    <td>Some Table Data</td>
  2.  
    <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 tag to get the single string within this tag.
  • If the tag has a single string child then the return value is that string.
  • If the tag has no children or more than one child the return value is None
  • If this tag has 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.  
    1、<td>some text</td>
  2.  
    2、<td></td>
  3.  
    3 、<td><p>more text</p></td>
  4.  
    4、<td>even <p>more text</p></td>
  5.  
     

.string on the four td will return,

  1.  
    1、some text
  2.  
    2、None
  3.  
    3、more text
  4.  
    4、None

.text will give result like this

  1.  
    1、some text
  2.  
     
  3.  
    2、more text
  4.  
    3、even more text

通过以上的举例,可以很清楚的发现,.find和.string之间的差异:

第一行,在指定标签td,没有子标签,且有文本时,两者的返回结果一致,都是文本

第二行,在指定标签td,没有子标签,且没有文本时,.string返回None,.text返回为空

第三行,在指定标签td,只有一个子标签时,且文本只出现在子标签之间时,两者返回结果一致,都返回子标签内的文本

第四行,最关键的区别,在指定标签td,有子标签,并且父标签td和子标签p各自包含一段文本时,两者的返回结果,存在很大的差异

.string返回为空,因为文本数>=2,string不知道获取哪一个

.text返回的是,两段文本的拼接。

bs4 string与text的区别的更多相关文章

  1. "text"和new String("text")的区别

    转自:What is the difference between “text” and new String(“text”)? new String("text"); expli ...

  2. jquery中html(), text(),val()区别(zhuan)

    https://zhidao.baidu.com/question/307317838.html http://www.cnblogs.com/aqbyygyyga/archive/2011/11/0 ...

  3. JavaScript toString、String和stringify方法区别

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  4. elasticsearch的keyword与text的区别

    es2.*用户可忽略该文章.es 2.*版本里面是没有这两个字段!!! 当初接触es,最惊讶就是他的版本速度发布太快,这次主要讨论keyword与text的区别 在es 2.*版本里面是没有这两个字段 ...

  5. String,StringBuffer,StringBuilder的区别

    public static void main(String[] args) { String str = new String("hello...."); StringBuffe ...

  6. JQuery中的html(),text(),val()区别

    jQuery中.html()用为读取和修改元素的HTML标签,.text()用来读取或修改元素的纯文本内容,.val()用来读取或修改表单元素的value值. 1.HTML html():取得第一个匹 ...

  7. JAVA中String与StringBuffer的区别

    String和StringBuffer的区别,网上资料可以说是数不胜数,但是看到这篇文章,感觉里面做的小例子很有代表性,所以转一下,并自己做了一点总结. 在java中有3个类来负责字符的操作. 1.C ...

  8. String 和 StringBuffer的区别

    String与StringBuffer的区别:            简单地说,就是一个常量和变量的关系.StringBuffer对象的内容可以修改:而String对象一旦产生后就不可以被修改,重新赋 ...

  9. [置顶] String StringBuffer StringBuilder的区别剖析

    这是一道很常见的面试题目,至少我遇到过String/StringBuffer/StringBuilder的区别:String是不可变的对象(final)类型,每一次对String对象的更改均是生成一个 ...

随机推荐

  1. 【SpringMVC】参数绑定

    一.概述 1.3 参数绑定过程 1.2 @RequestParam 二.自定义绑定使用属性编辑器 2.1 使用WebDataBinder(了解) 2.2 使用WebBindingInitializer ...

  2. 爬虫之 selenium模块

    selenium模块   阅读目录 一 介绍 二 安装 三 基本使用 四 选择器 五 等待元素被加载 六 元素交互操作 七 其他 八 项目练习 一 介绍 selenium最初是一个自动化测试工具,而爬 ...

  3. 【DRF框架】版本控制组件

    DRF框架提供的版本控制组件 核心代码:           version, scheme = self.determine_version(request, *args, **kwargs)req ...

  4. Flink原理(一)——基础架构

    Flink系列博客,基于Flink1.6,打算分为三部分:原理.源码.实例以及API使用分析,后期等系列博客完成后再弄一个目录. 该系列博客是我自己学习过程中的一些理解,若有不正确.不准确的地方欢迎大 ...

  5. 学习使用Django2 前台页面展示

    Django  2.1  python 3.7 创建一个虚拟环境 python  -m  venv  虚拟环境名称 进入虚拟环境 下载django pip install  django==2.1 创 ...

  6. JAVA 使用原生jdbc批量添加,表被锁住问题

    今天用jdbc批量添加数据的时候遇到了一个问题,当数据添加成功过后,再想对该表进行操作发现表被锁住了,检查了下代码发现事务提交了呀!!!!!!!!!!!! 去网上查了大半天的资料才发现问题,在conn ...

  7. 文件读写(一)利用File静态类 System.IO.FileInfo、DirectoryInfo、DriveInfo

    提供用于创建.复制.删除.移动和打开单一文件的静态方法,并协助创建 FileStream 对象. 一.读文件: 1.返回字符串:File.ReadAllText() string readText = ...

  8. H5--性能测试(PageSpeed Insights )

    中文网站(不需要翻墙): http://www.googlespeed.cn 主要可实现: 1.测试手机与电脑打开的速度对比. 2.详细的测试结果 3.直观的统计数据,查看页面的共xx个请求.总共大小 ...

  9. yum安装mysql(指定版)

    首先需要删除已经存在的mysql,不然后面会报错: 快速删除: yum remove mysql mysql-server mysql-libs mysql-server 查找残余文件: rpm -q ...

  10. 011——C#创建ecxel文件(附教程)

    (一)参考文献:[C#]创建表格(.xlsx)的典型方法 (二)视频教程:https://v.qq.com/x/page/t30068qfex5.html (三)下载地址:https://downlo ...