簡體   English   中英

C#如何獲取此信息?

[英]C# How I can retrieve this information?

在HTML頁面上,我有類似的內容

<table class="information">
<tbody>
<tr>
<td class="name">Name:</td>
<td><a href="example.com">John</a></td>
</tr>
<tr>
<td>...</td>
<td>...</td>
</tr>
....
</tbody>
</table>

如何檢索名稱(也有其他信息,但在我的示例中,我只寫了名稱)?

注意:HTML有多個表

我試過了

foreach (HtmlElement item in wb.Document.GetElementsByTagName("table"))
{
    if (item.OuterHtml.Contains("information"))
    {
        ... //Here i don't know how to continue
    }
} 
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

var table = doc.DocumentNode.SelectSingleNode("//table[@class='information']");
var td = table.SelectSingleNode("//td[@class='name']");

Console.WriteLine(td.InnerText);

要么

var text = doc.DocumentNode.Descendants("td")
    .First(td => td.Attributes["class"] != null && td.Attributes["class"].Value == "name")
    .InnerText;
HtmlElementCollection tData = wb.Document.GetElementsByTagName("td");

                foreach (HtmlElement td in tData)
                {
                    string name = "";
                    if (td.GetAttribute("classname") == "name")
                    {
                        name = td.InnerText;
                    }
                }

HtmlAgilityPack-它是免費的並且是很好的庫,可以使用html源代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM