2009-07-23 3 views
2

어떻게 이러한 루틴으로 구성된 테이블에서 정보를 추출 할 수 있습니까? nokogiri를 사용하여 주석을 감지 할 수 있습니까?ruby와 nokogiri를 사용하여 HTML 주석을 마커로 사용하여 HTML 구문 분석

<!-- 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

이 좀 더 구체적인 예를 제공 할 수 있습니까? 특정 입출력? –

+0

@Michael : 샘플 텍스트가 있으며 미리보기에는 표시되지만 질문에는 표시되지 않습니다. 지금 바로 고치십시오 ... –

답변

0

Nokogiri SAX Parser을 구현할 수 있습니다. 이것은 처음 보는 것보다 더 빨리 완료됩니다. 요소, 속성 및 주석에 대한 이벤트를 얻습니다.

구문 분석기 내에서 @currently_interested = true와 같이 상태를 기억하여 재사용 할 부분과 그렇지 않은 부분을 알아야합니다.

7

대신 XPath를 사용해보십시오 :

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

나는 이것이 가장 솔직하기 때문에 이것이 받아 들여진 해결책이라고 생각합니다. – thekingoftruth