Differences between .text and .get_text ()

Is there a difference between .text and .get_text() in BeautifulSoup ?

Which one should be preferred to get the text of the element?

 >>> 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' 
+5
source share
1 answer

It looks like .text is just a property that calls get_text . So calling get_text with no arguments is the same as .text . However, get_text can also support various keyword arguments to change their behavior ( separator , strip , types ). If you need more control over the result, you need a functional form.

+13
source

Source: https://habr.com/ru/post/1243361/


All Articles