在XPath中獲取所有td#

c# html html-agility-pack xpath

我試圖在C#中使用HtmlAgilityPack解析HTML。我有21 tr items ,每件物品有7 td items 。如何按順序獲取所有tr和td項目?現在我只能獲得一個tr項目及其7個td項目。

這是我的C#代碼:

var url = "url";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream());
            string sourceCode = sr.ReadToEnd();

            HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(sourceCode);


            var name = document.DocumentNode.SelectNodes("//*[@id=\"searchResultsTable\"]/tbody/tr[1]/td[2]/a[1]")[0].InnerText;
            var year = document.DocumentNode.SelectNodes("//*[@id=\"searchResultsTable\"]/tbody/tr[1]/td[3]")[0].InnerText;
            var km = document.DocumentNode.SelectNodes("//*[@id=\"searchResultsTable\"]/tbody/tr[1]/td[4]")[0].InnerText;
            var color = document.DocumentNode.SelectNodes("//*[@id=\"searchResultsTable\"]/tbody/tr[1]/td[5]")[0].InnerText;
           var price = document.DocumentNode.SelectNodes("//*[@id=\"searchResultsTable\"]/tbody/tr[1]/td[6]")[0].InnerText;
            var date = document.DocumentNode.SelectNodes("//*[@id=\"searchResultsTable\"]/tbody/tr[1]/td[7]")[0].InnerText;
            var location = document.DocumentNode.SelectNodes("//*[@id=\"searchResultsTable\"]/tbody/tr[1]/td[8]")[0].InnerText;

我試圖使用[@id=\"searchResultsTable\"]/tbody/tr[1]/td[position()<8]但只返回/ n

一般承認的答案

嘗試下面的代碼(未經過測試。編譯錯誤的機會。但是給你一個想法。)

代碼中的註釋為您提供了更多詳細信息。

//GET THE TABLE NODE
HtmlNode table = document.DocumentNode.SelectSingleNode("//*[@id='searchResultsTable']");

//LOOP THROUGH THE TABLE NODE AND FIND EACH TR 
foreach (HtmlNode row in table.SelectNodes("//tr")) {

      //PRINT HERE WHATEVER YOU WANT FOR EACH ROW.
      Console.WriteLine("New Row");

      //LOOP THROUGH THE ALL TD OF EACH TR
      foreach (HtmlNode cell in row.SelectNodes("//td")) {
          //PRINT HERE EACH TD
          Console.WriteLine("cell: " + cell.InnerText);
      } //END TD

}//END TR

熱門答案

與上面提到的類似,使用選擇器查詢以循環tr元素,然後選擇每行固定位置td節點:

假設有如下結構:

<table id="searchResultsTable">
<tbody>
<tr>
    <td>1</td>
    <td>Name<a>Name 1</a></td>
    <td>Year 1</td>
    <td>KM 1</td>
    <td>Color 1</td>
    <td>Price 1</td>
    <td>Date 1</td>
    <td>Location 1</td>
</tr>
<tr>
    <td>2</td>
    <td>Name<a>Name 2</a></td>
    <td>Year 2</td>
    <td>KM 2</td>
    <td>Color 2</td>
    <td>Price 2</td>
    <td>Date 2</td>
    <td>Location 2</td>
</tr>
</tbody>

例:

var document = new HtmlDocument();
document.Load("example.html");

var rows = document.DocumentNode.SelectNodes("//*[@id='searchResultsTable']/tbody/tr");

foreach(var row in rows)
{
    var name = row.SelectSingleNode("td[2]/a[1]").InnerText;
    var year = row.SelectSingleNode("td[3]").InnerText;
    var km = row.SelectSingleNode("td[4]").InnerText;
    var color = row.SelectSingleNode("td[5]").InnerText;
    var price = row.SelectSingleNode("td[6]").InnerText;
    var date = row.SelectSingleNode("td[7]").InnerText;
    var location = row.SelectSingleNode("td[8]").InnerText;

    Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}", name, year, km, color, price, date, location);
}

生產:

Name 1, Year 1, KM 1, Color 1, Price 1, Date 1, Location 1
Name 2, Year 2, KM 2, Color 2, Price 2, Date 2, Location 2



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