2012-07-23 18 views
5

Ho un html seguente e sto cercando di capire come posso dire a BeautifulSoup di estrarre td dopo un determinato elemento html. In questo caso voglio ottenere i dati in <td> dopoBeautifulSoup: come estrarre i dati dopo il tag html specifico

<tr> 
<td> Color Digest </td> 
<td> 2,36,156,38,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, </td> 
</tr> 

Questo è l'intero HTML

<html> 
<head> 
<body> 
<div align="center"> 
<table cellspacing="0" cellpadding="0" style="clear:both; width:100%;margin:0px; font-size:1pt;"> 
<br> 
<br> 
<table> 
<table> 
<tbody> 
<tr bgcolor="#AAAAAA"> 
<tr> 
<tr> 
<tr> 
<tr> 
<tr> 
<tr> 
<tr> 
<tr> 
<tr> 
<tr> 
<tr> 
<tr> 
<td> Color Digest </td> 
<td> 2,36,156,38,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, </td> 
</tr> 
</tbody> 
</table> 
+1

E 'questo tutto del codice HTML? O è in un file più grande con molti altri se s? Ed è garantito che ci sia solo un elemento "Color Digest" nell'html che stai analizzando? –

+0

No, questo è solo un frammento dell'html, quindi voglio davvero ottenere il meccanismo di ottenere l'elemento dopo un determinato elemento. Come in XPath si può dire che ho bisogno di prima td dopo Color Digest –

risposta

4

Suona come avete bisogno di iterare su una lista di <td> e smettere una volta che hai trovato la tua dati.

Esempio:

from BeautifulSoup import BeautifulSoup 

soup = BeautifulSoup('<html><tr><td>X</td><td>Color Digest</td><td>THE DIGEST</td></tr></html>') 
for cell in soup.html.tr.findAll('td'): 
    if 'Color Digest' == cell.text: 
     print cell.nextSibling.text 
Problemi correlati