.text和.get_text()之间的区别

时间:2022-04-22 21:47:14

In BeautifulSoup, is there any difference between .text and .get_text()?

在BeautifulSoup中,.text和.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'

1 个解决方案

#1


14  

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的属性。因此,不带参数调用get_text与.text相同。但是,get_text还可以支持各种关键字参数来更改其行为(分隔符,条带,类型)。如果您需要更多控制结果,那么您需要功能表单。

#1


14  

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的属性。因此,不带参数调用get_text与.text相同。但是,get_text还可以支持各种关键字参数来更改其行为(分隔符,条带,类型)。如果您需要更多控制结果,那么您需要功能表单。