使用HtmlAgilityPack獲取特定的行和列數據

c# html-agility-pack xml-parsing xpath

這是我的桌子

<table class="DataRows" frame="myFrames" rules="Standard" width="100%">

  <colgroup><col width="70" align="CENTER">
  <col width="200" align="LEFT">
  <col width="80" align="LEFT">
  <col align="LEFT">
  <col align="RIGHT">

  </colgroup><thead>

  <col width="70" align="CENTER">
  <col width="200" align="LEFT">
  <col width="80" align="LEFT">
  <col align="LEFT">
  <col align="RIGHT">

  <thead>

  <tr>
    <td valign="TOP"><span class="classicBold"> 20 </span> Kg.
    <td class="BOLD" valign="TOP" nowrap="">
      PA Passion Foods Inc.
    <td class="BOLD">Fax:
    <td>
      222-555666
    <td class="BOLD">
      Processed foods and juices

  <tr>
    <td><a target="_blank" href="">See on Map </a>
    <td>
      120 NW 157TH AVE 
    <td class="BOLD">Warehouse Hours:
    <td colspan="2">


  <tr>
    <td>
    <td><span class="BOLD">
      Jacksonville,
      </span>
      FL 300000
    <td class="BOLD">Url:
    <td colspan="2">
      <a target="_blank" href="">PA Passion</a>
      &nbsp&nbsp
      <span class="BOLD">E-mail:</span>
      zoro@xyz.com

  <tr>
    <td>
    <td class="REDBOLD" colspan="4">


  <tr>
    <td>
    <td colspan="4" align="LEFT">Franchisee for:<span class="BOLD">
 Nutrella


</span>
  <tr>
    <td>
    <td colspan="4" align="LEFT">Franchisee for:<span class="BOLD">
APPLE Foods, Constants
</span>
  <tr>
    <td>
    <td colspan="4" align="LEFT"><span class="BOLD">

</span>

  <tr>
    <td>
    <td colspan="4" align="LEFT">We service:<span class="BOLD">
All occasions and hospitality services
</span>

  <tr>
    <td>
    <td colspan="4" align="LEFT">We sell :<span class="BOLD">
----
</span>

</td></td></tr></td></td></tr></td></td></tr></td></td></tr></td></td></tr></td></td></tr></td></td></td></td></tr></td></td></td></td></tr></td></td></td></td></td></tr>
  </thead>
</table>

我使用下面的代碼循環遍歷我的Html文檔中的每個節點

foreach (HtmlNode node in htmlAgilityPackDoc.DocumentNode.SelectNodes("//table[contains(@class,'DataRows')]"))
{

}

當我使用以下內容時

node.SelectSingleNode(".//tr[1]/td[1]").InnerHtml

我得到以下HTML

<span class="classicBold"> 20 </span> Kg.
        <td class="BOLD" valign="TOP" nowrap="">
          PA Passion Foods Inc.
        <td class="BOLD">Fax:
        <td>
          222-555666
        <td class="BOLD">
          Processed foods and juices

      <tr>
        <td><a target="_blank" href="">See on Map </a>
        <td>
          120 NW 157TH AVE 
        <td class="BOLD">Warehouse Hours:
        <td colspan="2">


      <tr>
        <td>
        <td><span class="BOLD">
          Jacksonville,
          </span>
          FL 300000
        <td class="BOLD">Url:
        <td colspan="2">
          <a target="_blank" href="">PA Passion</a>
          &nbsp&nbsp
          <span class="BOLD">E-mail:</span>
          zoro@xyz.com

      <tr>
        <td>
        <td class="REDBOLD" colspan="4">


      <tr>
        <td>
        <td colspan="4" align="LEFT">Franchisee for:<span class="BOLD">
     Nutrella


    </span>
      <tr>
        <td>
        <td colspan="4" align="LEFT">Franchisee for:<span class="BOLD">
    APPLE Foods, Constants
    </span>
      <tr>
        <td>
        <td colspan="4" align="LEFT"><span class="BOLD">

    </span>

      <tr>
        <td>
        <td colspan="4" align="LEFT">We service:<span class="BOLD">
    All occasions and hospitality services
    </span>

      <tr>
        <td>
        <td colspan="4" align="LEFT">We sell :<span class="BOLD">
    ----
    </span>

    </td></td></tr></td></td></tr></td></td></tr></td></td></tr></td></td></tr></td></td></tr></td></td></td></td></tr></td></td></td></td></tr></td></td></td></td></td>

如何從中提取地址120 NW 157TH AVE

當我嘗試使用時

node.SelectSingleNode(".//td[@class='BOLD'][4]/preceding-sibling::td").InnerText;

我收到一個錯誤:

你調用的對像是空的

一般承認的答案

你的HTML是亂七八糟的標籤重疊我建議您使用文本節點作為您的標識符,而不是索引

.//td[./a[contains(text(),'See on Map')]]/td/text() 

要得到

120 NW 157TH AVE

這是一個完整的例子,可以為您提供一切

    var table = doc.DocumentNode.SelectSingleNode("//table[contains(@class,'DataRows')]");

    var name = table.SelectSingleNode(".//td[@class='BOLD']/text()").InnerText.Trim();
    var fax = table.SelectSingleNode(".//td[contains(text(),'Fax')]/td/text()").InnerText.Trim();
    var email = table.SelectSingleNode(".//span[contains(text(),'E-mail')]/following-sibling::text()").InnerText.Trim();
    var address = table.SelectSingleNode(".//td[./a[contains(text(),'See on Map')]]/td/text()").InnerText.Trim();
    var city = table.SelectSingleNode(".//tr[./td/a[contains(text(),'See on Map')]]//tr/td/td/span").InnerText.Trim(',');
    var zip = table.SelectSingleNode(".//tr[./td/a[contains(text(),'See on Map')]]//tr/td/td/span/following-sibling::text()").InnerText.Trim();

請注意,因為你的html有多亂,xpath必須是亂的,試圖通過索引訪問tr元素是行不通的,因為所有tr元素都是前一個tr子元素,什麼是.//tr[4]普通表是.//tr/tr/tr/tr在你的表中。




許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因