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对象的更改均是生成一个 ...
随机推荐
- linux uniq命令用法
uniq命令: 对指定的ASCII文件或标准输入进行唯一性检查,以判断文本文件中重复出现的行,常用于分析日志:查看tcp各个状态连接数,ip或域名连接数排名等等场景,一般与 sort 命令结合使用. ...
- rabbitmq笔记(一)rabbitmq简介及基础
一.消息组件 如果从消息组件来讲主要划分位两类: 1.JMS组件:ActiveMQ(慢): 2.AMQP组件(协议):性能是最高的, 而AMQP有两个主要的开源: 1)RabbitMQ:使用最广泛,速 ...
- ubuntu18.04 下启动Android Studio报错KVM is required to run this AVD. /dev/kvm device: permission denied.
在ubuntu18.04下安装Android Studio,安装了模拟器后运行报错 KVM is required to run this AVD. /dev/kvm device: permissi ...
- Android笔记(十七) Android中的Service
定义和用途 Service是Android的四大组件之一,一直在后台运行,没有用户界面.Service组件通常用于为其他组件提供后台服务或者监控其他组件的运行状态,例如播放音乐.记录地理位置,监听用户 ...
- Linux命令——yum
翻译自:20 Linux YUM (Yellowdog Updater, Modified) Commands for Package Management 前言 本篇文章将介绍如何使用RedHat开 ...
- 利用shell命令分析服务器日志
在没有专业日志分析系统的情况下,我们有时需要对日志进行简单的分析,下面列出一些常用的shell命令分析日志的方法,一定要收藏 1.查看有多少个ip访问 awk '{print $1}' log_f ...
- Warning | 3719 | 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
MySQL的“utf8”实际上不是真正的UTF-8.“utf8”只支持每个字符最多三个字节,而真正的UTF-8是每个字符最多四个字节. MySQL一直没有修复这个bug,他们在2010年发布了一个叫作 ...
- java项目中注解使用——整理
文章:@Mapper注解的使用 地址:https://blog.csdn.net/weixin_39666581/article/details/81057385 @Mapper注解的的作用 1:为了 ...
- 2019-ACM-ICPC-徐州站网络赛- I. query-二维偏序+树状数组
2019-ACM-ICPC-徐州站网络赛- I. query-二维偏序+树状数组 [Problem Description] 给你一个\([1,n]\)的排列,查询\([l,r]\)区间内有多少对 ...
- Arduino Tian开发板:一个功能强大的天气预报中心
每天都在出现新的连接设备. Arduino携手云平台一起加入这场战斗,于是出现了一个新的挑战者 - Arduino Tian! 使用python和经典Arduino框架,本教程将引导您将您的Ardui ...