Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between .text and .get_text()

Tags:

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'
like image 968
alecxe Avatar asked Feb 19 '16 02:02

alecxe


1 Answers

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.

like image 127
mgilson Avatar answered Sep 22 '22 01:09

mgilson