BitByBit BitByBit - 1 year ago 322
Python Question

Using BeautifulSoup, how to get text only from the specific selector without the text in the children?

I don't know how to code BeautifulSoup so that it gives me only the text from the selected tag. I get more such as the text of its child(ren)!

For example:

from bs4 import BeautifulSoup
soup = BeautifulSoup('<div id="left"><ul><li>"I want this text"<a href=""> I don\'t want this text</a><p>I don\'t want this either</li><li>"Good"<a href=""> Not Good</a><p> Not Good either</li></ul></div>', "html5lib")
x ='ul > li')
for i in x:


"I want this text" I don't want this textI don't want this either

"Good" Not Good Not Good either

Desired Output:

"I want this text"


Answer Source

One option would be to get the first element of the contents list:

for i in x:

Another - find the first text node:

for i in x:

Both would print:

"I want this text"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download