.text 和 .get_text() 之间的差异 [英] Differences between .text and .get_text()
本文介绍了.text 和 .get_text() 之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在BeautifulSoup
中,.text
和.get_text()
有什么区别吗?
获取元素的文本应该首选哪一个?
<预><代码>>>>从 bs4 导入 BeautifulSoup>>>>>>html = "<div>text1 <span>text2</span><div>">>>汤 = BeautifulSoup(html, "html.parser")>>>div = 汤.div>>>div.text'文本 1 文本 2'>>>div.get_text()'文本 1 文本 2' 解决方案
看起来像 .text
只是一个调用 get_text
的属性.因此,不带参数调用get_text
与.text
是一回事.但是,get_text
还可以支持各种关键字参数来改变它的行为方式(separator
、strip
、types
).如果您需要更多地控制结果,那么您需要函数形式.
In BeautifulSoup
, is there any difference between .text
and .get_text()
?
Which one should be preferred for getting element's text?
>>> from bs4 import BeautifulSoup
>>>
>>> html = "<div>text1 <span>text2</span><div>"
>>> soup = BeautifulSoup(html, "html.parser")
>>> div = soup.div
>>> div.text
'text1 text2'
>>> div.get_text()
'text1 text2'
解决方案
It looks like .text
is just a property that calls get_text
. Therefore, calling get_text
without arguments is the same thing as .text
. However, get_text
can also support various keyword arguments to change how it behaves (separator
, strip
, types
). If you need more control over the result, then you need the functional form.
这篇关于.text 和 .get_text() 之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文