kharandziuk
kharandziuk

Reputation: 12910

Parsing html in Beautiful soup

I try to parse the fragments of html like this:

<div><span>adrress</span>text of address</div>

How can I take fragment 'text of address' programatically without span tag in Beatiful soup?

Now I take whole content of div and remove span, but I think there are a better way

Upvotes: 1

Views: 469

Answers (2)

falsetru
falsetru

Reputation: 369424

>>> fragment = '<div><span>adrress</span>text of address</div>'
>>> soup = BeautifulSoup(fragment)
>>> soup.div.span.nextSibling
u'text of address'

Upvotes: 1

zmo
zmo

Reputation: 24802

here's how:

>>> from BeautifulSoup import BeautifulSoup
>>> text = "<div><span>address</span>text of address</div>"
>>> print BeautifulSoup(text).find('div').contents[-1]
text of address

Sorry for my first answer, I misread the question.

Upvotes: 0

Related Questions