2013-11-24 26 views
3

的HtmlHtmlAgilityPack基本如何獲得標題和鏈接?

<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 

我想獲得一個divSelectSingleNode取回複製的價值linktitle ..

如何獲得真實的。

+0

使用HtmlAgilityPack lib是必要的必要條件?我可以給你一個使用正則表達式的解決方案。 – ElektroStudios

回答

1

如果你看到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 
2

這是正確的用法:

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

@ Gaby,@ dotNET感謝您的幫助! – user2989391

+1

嘿,那我爲一個陌生人作出賞金呢? :P @dotNET感謝您的解決方案。 – ElektroStudios

+0

@ElektroStudios也謝謝你:) – user2989391