【问题标题】:HtmlAgilityPack returns only first matchHtmlAgilityPack 仅返回第一个匹配项
【发布时间】:2014-05-30 13:45:18
【问题描述】:
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007">
    <id>http://gdata.youtube.com/feeds/api/users/upvZG-5ko_eiXAupbDfxWw</id>
    <published>2005-10-02T16:06:36.000Z</published>
    <updated>2014-05-29T20:18:58.000Z</updated>
    <category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#userProfile"/>
    <category scheme="http://gdata.youtube.com/schemas/2007/channeltypes.cat" term="DIRECTOR"/>
    <title type="text">CNN</title>
    <content type="text">CNN operates as a division of Turner Broadcasting System, which is a subsidiary of Time Warner. CNN identifies itself as -- and is widely known to be - the most trusted source for news and information. The CNN umbrella includes nine cable and satellite television networks, two radio networks, the CNN Digital Network, which is the top network of news Web sites in the United States, and CNN Newsource, the world's most extensively syndicated news service. CNN is proud of our ability to bring you up-to-the-minute news from around the world, as a result of our many extensions.</content>
    <link rel="alternate" type="text/html" href="http://www.youtube.com/channel/UCupvZG-5ko_eiXAupbDfxWw"/>
    <link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/upvZG-5ko_eiXAupbDfxWw"/>
    <author>
        <name>CNN</name>
        <uri>http://gdata.youtube.com/feeds/api/users/CNN</uri>
    </author>
    <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.subscriptions" href="http://gdata.youtube.com/feeds/api/users/cnn/subscriptions" countHint="6"/>
    <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.contacts" href="http://gdata.youtube.com/feeds/api/users/cnn/contacts" countHint="4250"/>
    <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.inbox" href="http://gdata.youtube.com/feeds/api/users/cnn/inbox"/>
    <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.playlists" href="http://gdata.youtube.com/feeds/api/users/cnn/playlists"/>
    <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.uploads" href="http://gdata.youtube.com/feeds/api/users/cnn/uploads" countHint="58269"/>
    <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.newsubscriptionvideos" href="http://gdata.youtube.com/feeds/api/users/cnn/newsubscriptionvideos"/>
    <yt:location>US</yt:location>
    <yt:statistics lastWebAccess="1970-01-01T00:00:00.000Z" subscriberCount="484272" videoWatchCount="0" viewCount="0" totalUploadViews="400764857"/>
    <media:thumbnail url="http://i1.ytimg.com/i/upvZG-5ko_eiXAupbDfxWw/1.jpg?v=51df0b06"/>
    <yt:username>cnn</yt:username>
</entry>

我试过这段代码,但它只返回第一个匹配,我需要 countHint 的值等于 58269 in

<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.uploads" href="http://gdata.youtube.com/feeds/api/users/cnn/uploads" countHint="58269"/>

我的代码:

Dim request As HttpWebRequest = WebRequest.Create("http://gdata.youtube.com/feeds/api/users/cnn")
Dim response As HttpWebResponse = request.GetResponse()
Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
Dim page As String = reader.ReadToEnd()
Dim dokuman = New HtmlAgilityPack.HtmlDocument()
dokuman.LoadHtml(page)
Dim gets As HtmlAgilityPack.HtmlNodeCollection = dokuman.DocumentNode.SelectNodes("//entry")
For Each node In gets
    If node.ChildNodes("gd:feedLink").Attributes("href").Value = "http://gdata.youtube.com/feeds/api/users/cnn/uploads" Then
        '......
    End If
Next

如何使用 HtmlAgiltyPack 获得 58269 的值?如果我尝试

dokuman.DocumentNode.SelectNodes("//gd:feedLink") 

然后抛出错误!

【问题讨论】:

    标签: xml vb.net youtube html-agility-pack


    【解决方案1】:

    好像是HtmlAgilityPack doesn't support XPath containing prefix。您可以通过选择 &lt;entry&gt; 元素的所有子元素来解决此限制,然后使用 LINQ 按节点名称缩小结果范围:

    Dim result As HtmlNode = _
            dokuman.DocumentNode _
               .SelectNodes("//entry/*") _
               .FirstOrDefault(Function(x) _
                                   x.Name = "gd:feedlink" AndAlso _
                                   x.Attributes("href").Value = "http://gdata.youtube.com/feeds/api/users/cnn/uploads")
    If Not result Is Nothing Then
        'this is a better practice for accessing attribute value from HtmlNode...'
        '...to avoid exception if such attribute not found'
        Console.WriteLine(result.GetAttributeValue("countHint", "not found"))
    End If
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      相关资源
      最近更新 更多