使用HTMLAgilityPack提取所有值

时间:2013-12-01 12:54:21

标签: html-agility-pack

以下是网页的HTML,我愿意提取所有数据用户ID字段的值

<div class="account   js-actionable-user js-profile-popup-actionable " data-screen-name="ducttape" data-user-id="817977">
<div class="account   js-actionable-user js-profile-popup-actionable " data-screen-name="adage" data-user-id="12480582">

任何人都可以提供帮助是c#或vb.net我试过跟随代码,但它不起作用

Dim UrlRegister1 As String = "C:/Users/Shripad/Desktop/result.htm"
   Dim document = New HtmlDocument()
    document.Load("C:/Users/Shripad/Desktop/result.htm")
    Dim node As HtmlNode = document.DocumentNode.SelectSingleNode("//class='account   js-actionable-user js-profile-popup-actionable'/data-user-id=")
    Dim useridd As String = node.Attributes("Value").Value

1 个答案:

答案 0 :(得分:0)

为了选择具有'account'类的元素 - 使用XPath包含这样的选择器:

[contains(@class, 'account')]

整个结果应该是:

var node = doc.DocumentNode.SelectSingleNode(@"//div[contains(@class, 'account')]");
var useridd = node.Attributes["data-user-id"].Value;

以下是有关按类选择元素的更多信息:XPath for CSS classes