我正在尝试从代码中的网页中提取网址。 使用此代码它可以工作,但它给了我几次所有网址。 我只需要在文本框中输入一次。
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("https://www.youtube.com/results?search_query=test+search")
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("a")
For Each curElement As HtmlElement In theElementCollection
If curElement.GetAttribute("href").Contains("/watch") Then
TextBox1.Text += curElement.GetAttribute("href") & vbCrLf
End If
Next
End Sub
任何帮助表示感谢。
答案 0 :(得分:0)
您可以使用HashSet(Of String)
+ String.Join
:
Dim uniqueUrls As New HashSet(Of String)
For Each curElement As HtmlElement In theElementCollection
If curElement.GetAttribute("href").Contains("/watch") Then
uniqueUrls.Add(curElement.GetAttribute("href"))
End If
Next
TextBox1.Text = String.Join(vbCrLf, uniqueUrls)