对于此示例,如何使用 htmlagilitypack 从 HTML 中提取文本?

发布于 2024-11-04 23:15:13 字数 779 浏览 0 评论 0原文

我想从 HTML 源中提取文本。我正在尝试使用 c# 和 htmlagilitypack dll。

来源是:

<table>
  <tr>
    <td class="title">
      <a onclick="func1">Here 2</a>
    </td>
    <td class="arrow">
      <img src="src1" width="9" height="8" alt="Down">
    </td>
    <td class="percent">
      <span>39%</span>
    </td>
    <td class="title">
      <a onclick="func2">Here 1</a>
    </td>
    <td class="arrow">
      <img src="func3" width="9" height="8" alt="Up">
    </td>
    <td class="percent">
      <span>263%</span>
    </td>
  </tr>
</table>

How can I get the text Here 1 and Here 2 from the table?

I wanna extract the text from a HTML source. I'm trying with c# and htmlagilitypack dll.

The source is:

<table>
  <tr>
    <td class="title">
      <a onclick="func1">Here 2</a>
    </td>
    <td class="arrow">
      <img src="src1" width="9" height="8" alt="Down">
    </td>
    <td class="percent">
      <span>39%</span>
    </td>
    <td class="title">
      <a onclick="func2">Here 1</a>
    </td>
    <td class="arrow">
      <img src="func3" width="9" height="8" alt="Up">
    </td>
    <td class="percent">
      <span>263%</span>
    </td>
  </tr>
</table>

How can I get the text Here 1 and Here 2 from the table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

超可爱的懒熊 2024-11-11 23:15:13
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml("web page string");
var xyz = from x in htmlDoc.DocumentNode.DescendantNodes()
                     where x.Name == "td" && x.Attributes.Contains("class")
                     where x.Attributes["class"].Value == "title"
                     select x.InnerText;

不太漂亮但应该可以

HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml("web page string");
var xyz = from x in htmlDoc.DocumentNode.DescendantNodes()
                     where x.Name == "td" && x.Attributes.Contains("class")
                     where x.Attributes["class"].Value == "title"
                     select x.InnerText;

not so pretty but should work

一个人的夜不怕黑 2024-11-11 23:15:13

Xpath 版本

 HtmlDocument doc = new HtmlDocument();
 doc.LoadHtml(t);

 //this simply works because InnerText is iterative for all child nodes
 HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//td[@class='title']");
//but to be more accurate you can use the next line instead
//HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//td[@class='title']/a");


 string result;
 foreach (HtmlNode item in nodes) 
       result += item.InnerText;

和 LINQ 版本只需更改 var Nodes = .. 行:

 var Nodes = from x in htmlDoc.DocumentNode.DescendantNodes()
                  where x.Name == "td" && x.Attributes["class"].Value == "title"
                  select x.InnerText;

Xpath version

 HtmlDocument doc = new HtmlDocument();
 doc.LoadHtml(t);

 //this simply works because InnerText is iterative for all child nodes
 HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//td[@class='title']");
//but to be more accurate you can use the next line instead
//HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//td[@class='title']/a");


 string result;
 foreach (HtmlNode item in nodes) 
       result += item.InnerText;

and for the LINQ version just change the var Nodes = .. line with:

 var Nodes = from x in htmlDoc.DocumentNode.DescendantNodes()
                  where x.Name == "td" && x.Attributes["class"].Value == "title"
                  select x.InnerText;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文