【问题标题】:HtmlAgilityPack basic how to get title and link?HtmlAgilityPack 基本如何获取标题和链接?
【发布时间】:2013-11-24 19:42:43
【问题描述】:

HTML

<div class="col">                   
<a class="video-box" title="En son haber" href="http://**/en-son-haber">
<img class="img-responsive" alt="en son haber" src="http://**/thumb/6/9/6/200x120/en-son-haber-49-29.jpg"> 
<span class="title">En son haber</span>
<span class="duration">01:02</span><span class="view-count">9.023</span></a>
</div>

代码

 Dim request2 As HttpWebRequest = WebRequest.Create("http://**.com/")
 Dim response2 As HttpWebResponse = request2.GetResponse()
 Dim reader2 As StreamReader = New StreamReader(response2.GetResponseStream())
 Dim sayfa2 As String = reader2.ReadToEnd()
 Dim dokuman2 = New HtmlAgilityPack.HtmlDocument()                         
 dokuman2.LoadHtml(sayfa2)

 Dim getir2 As HtmlAgilityPack.HtmlNodeCollection = dokuman2.DocumentNode.SelectNodes("//div[@class='col']")
 For Each node In getir2             
      TextBox1.Text += node.SelectSingleNode("//a[@class='video-box']").SelectSingleNode("href").InnerText 
 Next

我想在 div 中获取 linktitleSelectSingleNode 检索重复值..

如何实现。

【问题讨论】:

  • 使用 HtmlAgilityPack 库是必要的吗?我可以给你一个使用正则表达式的解决方案。

标签: html vb.net parsing


【解决方案1】:

这是正确的用法:

TextBox1.Text &= node.SelectSingleNode("//a[@class='video-box']").Attributes("title").Value
TextBox1.Text &= node.SelectSingleNode("//a[@class='video-box']").Attributes("href").Value

【讨论】:

  • @Gaby,@dotNET 感谢您的帮助!
  • 嘿,我为一个不知名的人做赏金呢? :P @dotNET 感谢您的解决方案。
【解决方案2】:

如果您看到示例页面http://htmlagilitypack.codeplex.com/wikipage?title=Examples,第一个示例显示了如何访问属性..

在你的情况下

 For Each node In getir2   
      dim aTag as HtmlAgilityPack.HtmlNode = node.SelectSingleNode("//a[@class='video-box']")
      TextBox1.Text += aTag["href"].value
      'and for the title
      TextBox1.Text += aTag["title"].value
 Next

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 2012-12-28
    • 2012-11-06
    • 2011-08-11
    • 2016-12-30
    • 2013-05-23
    • 1970-01-01
    相关资源
    最近更新 更多