编程语言
首页 > 编程语言> > c# – 如何使用htmlagilitypack为此示例从HTML中提取文本?

c# – 如何使用htmlagilitypack为此示例从HTML中提取文本?

作者:互联网

我想从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>

如何从表中获取文本Here 1和Here 2?

解决方法:

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;

不是很漂亮,但应该工作

标签:c,linq,xpath,html-agility-pack,html-content-extraction
来源: https://codeday.me/bug/20190721/1496130.html