BeautifulSoup to get string inner 'p' and 'a' tags

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Jul 24 06:17:01 EDT 2006


In <1153734198.020895.269760 at i42g2000cwa.googlegroups.com>, GinTon wrote:

> I'm trying to get the 'FOO' string but the problem is that inner 'P'
> tag there is another tag, 'a'. So:
> 
>> from BeautifulSoup import BeautifulSoup
>> s = '<td width="88%" valign="TOP"> <p class="contentBody">FOO <a name="f"></a> </p></td>'
>> tree = BeautifulSoup(s)
> 
>> print tree.first('p')
> <p class="contentBody">FOO <a name="f"></a> </p>
> 
> So if I run 'print tree.first('p').string' to get the 'FOO' string it
> shows Null value because it's the 'a' tag:
> 
>> print tree.first('p').string
> Null
> 
> Any solution?

In [53]: print tree.first('p').contents[0]
FOO

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list