2015-06-11 33 views
-2

我想在列表框中添加項目:如何獲得vb.net中添加特定的div類中的所有鏈接從一個網頁

https://www.facebook.com/XXXXXXX 

https://www.facebook.com/XXXXXXX 

文件:

<div class="fsl fwb fcb"> 
<a href="https://www.facebook.com/XXXXXXX?fref=pb&hc_location=friends_tab" 
<div class="fsl fwb fcb"> 
<a href="https://www.facebook.com/XXXXXXX?fref=pb&hc_location=friends_tab" 
<div class="fsl fwb fcb"> 
<a href="https://www.facebook.com/XXXXXXX?fref=pb&hc_location=friends_tab" 
+0

好吧,這是什麼問題? –

回答

1

該作品如預期的那樣:

Dim html = File.ReadAllText("Path") 
Dim doc = New HtmlAgilityPack.HtmlDocument() 
doc.LoadHtml(html) 

Dim anchorTexts As New List(Of String) 
Dim divNodes = doc.DocumentNode.SelectNodes("//div[@class='fsl fwb fcb']") 
If Not divNodes Is Nothing Then 
    For Each div In divNodes 
     For Each anchorNode In div.SelectNodes("//a") 
      Dim href As String = anchorNode.GetAttributeValue("href", "") 
      If Not String.IsNullOrEmpty(href) Then 
       anchorTexts.Add(href) 
      End If 
     Next 
    Next 
End If 
+0

Dim html = File.ReadAllText(「Path」)file not found。我想listbox1.items.add(href)? –

+0

@YavuzSelimÖzmen:當然找不到它。對不起,如果你已經熟悉'HtmlAgilityPack'這個建議你更有經驗的人。我已經使用它,因爲我需要一種方法來將您的示例字符串作爲文本。最簡單的方法是使用文件。所以,只需用你的字符串替換Dim html = File.ReadAllText(「Path」)。根據問題,你如何得到列表介紹一個'ListBox':使用一個循環並添加項目或將其用作數據源。 –

+0

我不能。你是代碼嗎? –

相關問題