2012-03-09 17 views
3

如何使用Nokogiri在<!--Sanction 3-->(解析HTML)中查找文本?根據HTML查找文本評論

我在網站中輸入搜索詞,結果顯示在下一頁上。如果符合特定條件,我需要以編程方式從結果頁面獲取數據。

當我分析結果頁面時,我注意到物品被分解爲制裁。我需要知道制裁是否有數據,如果有,是否包含我的關鍵字;我正在尋找縣/州。我不知道如何讓它看起來是一種制裁。以下是一些HTML代碼:

<!--Sanction 3--> 

<table border="2" cellpadding="2" cellspacing="0" width="100%"> 
    <thead> 
     <tr> 
      <th class="XXheaderClass" colspan="5" scope="colgroup"> 
       <table bgcolor="#ff9999" width="100%"> 
        <tbody> 
         <tr> 
          <td class="XXsanctionHeader1"> 
           <span class="XXtextBold">Requirements Met</span> 
          </td> 
          <td class="XXsanctionHeader2"> 
           <span class="XXtextBold">Status: GOOD</span> 
          </td> 
         </tr> 
        </tbody> 
       </table> 
      </th> 
     </tr> 
     <tr> 
      <th class="XXheaderClass" width="31%" scope="col"> 
       <span class="XXsmallTextBold">Description</span> 
      </th> 
      <th class="XXheaderClass" width="12%" scope="col"> 
       <span class="XXsmallTextBold">Effective Date</span> 
      </th> 
      <th class="XXheaderClass" width="12%" scope="col"> 
       <span class="XXsmallTextBold">Number</span> 
      </th> 
      <th class="XXheaderClass" width="12%" scope="col"> 
       <span class="XXsmallTextBold">County/State</span> 
      </th> 
      <th class="XXheaderClass" width="33%" scope="col"> 
       <span class="XXsmallTextBold">Address and Phone Number</span> 
      </th> 
     </tr> 
    </thead> 
    <tfoot> 
     <tr> 
      <td class="footerClass" colspan="5"> 
       <table class="panelBox"> 
        <tr> 
         <td> 
          <a href="SanctionHelpPages/Sanction03Help.aspx" id="MainContent_lvSanction3_sanction03Link" class="outputLinkEx"><span class="XXlinkBold"> 
            Click Here</span></a> 
         </td> 
         <td> 
          <span class="XXtextBold">to resolve, 
           requirements met.</span> 
         </td> 
        </tr> 
       </table> 
      </td> 
     </tr> 
    </tfoot> 
    <tbody> 

     <tr id="MainContent_lvSanction3_Tr1_0"> 
      <td class="XXsmallText"> 
       <span id="MainContent_lvSanction3_lblDescription_0">DESCRIPTION  </span> 
      </td> 
      <td class="XXsmallText"> 
       <span id="MainContent_lvSanction3_lblEffectiveDate_0">9/19/20011</span> 
      </td> 
      <td class="XXsmallText"> 
       <span id="MainContent_lvSanction3_lblNumber_0">1111    </span> 
      </td> 
      <td class="XXsmallText"> 
       <span id="MainContent_lvSanction3_lblCountyState_0">MyCounty  </span> 
      </td> 
      <td class="XXsmallText"> 
       <span id="MainContent_lvSanction3_lblAddressAndPhoneNumber_0">1234 MyRoad AVE. CITY          (xxx)xxx-xxxx</span> 
      </td> 
     </tr> 

    </tbody> 
</table> 
<br /> 
+0

因此,您正在尋找從表*中提取數據單元格*僅限於「制裁」3 *其中「CountyState」包含您的搜索項?你需要提取哪些數據單元?其他「制裁」包含什麼? – 2012-03-12 12:21:59

+0

@MarkThomas我還需要來自其他制裁的信息。其他制裁與上述相同。根據搜索的情況,制裁可能會或可能不會。根據搜索結果,可能會有一個或多個「CountyState」。 – user1128637 2012-03-12 15:49:09

+0

我們應該如何確定你想要的信息?你是否想要包含匹配CountyState的所有制裁? – 2012-03-12 16:23:20

回答

6

您可以使用doc.xpath("//comment()")查找所有評論節點。然後,您可以遍歷這些節點並檢查他們的兄弟姐妹的數據。儘管沒有更多的信息,但要給出全面的答案有點困難。

+3

+1主要是約定。我會忽略註釋節點並查看底層表,在那裏搜索可識別所需內容的文本。作爲最後的手段,我會尋找評論。但那只是我。 – 2012-03-09 22:42:13

+0

非常感謝你們! – user1128637 2012-03-13 15:44:45