2009-07-23 8 views
2

Comment utiliser ruby ​​pour extraire des informations d'une table composée de ces lignes? Est-il possible de détecter les commentaires en utilisant nokogiri?Utiliser ruby ​​et nokogiri pour analyser HTML en utilisant des commentaires HTML comme marqueurs

<!-- Begin Topic Entry 4134 --> 
    <tr> 
     <td align="center" class="row2"><image src='style_images/ip.boardpr/f_norm.gif' border='0' alt='New Posts' /></td> 
     <td align="center" width="3%" class="row1">&nbsp;</td> 
     <td class="row2"> 
      <table class='ipbtable' cellspacing="0"> 
       <tr> 

<td valign="middle"><alink href='http://www.xxx.com/index.php?showtopic=4134&amp;view=getnewpost'><image src='style_images/ip.boardpr/newpost.gif' border='0' alt='Goto last unread' title='Goto last unread' hspace=2></a></td> 

        <td width="100%"> 
        <div style='float:right'></div> 
        <div> <alink href="http://www.xxx.com/index.php?showtopic=4134&amp;hl=">EXTRACT LINK 1</a> </div> 
        </td> 
       </tr> 
      </table> 
      <span class="desc">EXTRACT DESCRIPTION</span> 
     </td> 
     <td class="row2" width="15%"><span class="forumdesc"><alink href="http://www.xxx.com/index.php?showforum=19" title="Living">EXTRACT LINK 2</a></span></td> 
     <td align="center" class="row1" width='10%'><alink href='http://www.xxx.com/index.php?showuser=1642'>Mr P</a></td> 
     <td align="center" class="row2"><alink href="javascript:who_posted(4134);">1</a></td> 
     <td align="center" class="row1">46</td> 
     <td class="row1"><span class="desc">Today, 12:04 AM<br /><alink href="http://www.xxx.com/index.php?showtopic=4134&amp;view=getlastpost">Last post by:</a> <b><alink href='http://www.xxx.com/index.php?showuser=1649'>underft</a></b></span></td> 
    </tr> 
<!-- End Topic Entry 4134 --> 
--> 
+0

Pouvez-vous donner un exemple plus concret? Entrée et sortie spécifiques? –

+0

@Michael: Il y a un exemple de texte, et il apparaît dans l'aperçu mais pas dans la question. Corriger maintenant ... –

Répondre

0

Vous pouvez implémenter un Nokogiri SAX Parser. Ceci est fait plus rapidement que cela pourrait sembler à première vue. Vous obtenez des événements pour les éléments, les attributs et les commentaires.

Dans votre analyseur, vous devriez vous rappeler l'état, comme @currently_interested = true pour savoir quelles parties doivent être remémorées et lesquelles ne le sont pas.

7

Essayez d'utiliser XPath à la place:

html_doc = Nokogiri::HTML("<html><body><!-- Begin Topic Entry 4134 --></body></html>") 
html_doc.xpath('//comment()') 
+1

Je crois que cela devrait être la solution acceptée, car c'est la plus simple. – thekingoftruth

Questions connexes