Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get text outside one tag and inside another

I am parsing a web page with BeautifulSoup, and it has some elements like the following:

<td><font size="2" color="#00009c"><b>Consultant Registration Number  :</b></font>  16043646</td>

The structure always seems to be a <td> with the first part surrounded by <font><b>, and the text after the </font> tag can be empty. How can I get that text that is after the font tag?

In this example I would want to get "16043646". If the html was instead

<td><font size="2" color="#00009c"><b>Consultant Registration Number  :</b></font></td>

I would want to get ""

like image 472
murgatroid99 Avatar asked Aug 25 '11 16:08

murgatroid99


1 Answers

>>> from BeautifulSoup import BeautifulSoup
>>> text1 = '<td><font size="2" color="#00009c"><b>Consultant Registration Number  :</b></font>  16043646</td>'
>>> text2 = '<td><font size="2" color="#00009c"><b>Consultant Registration Number  :</b></font></td>'
>>> BeautifulSoup(text1).td.font.nextSibling
u'  16043646'
>>> BeautifulSoup(text2).td.font.nextSibling
>>>
like image 181
Shawn Chin Avatar answered Sep 22 '22 16:09

Shawn Chin