大家好,我试图从网站抓取特定链接并将其添加到列表框中。
我有两个问题:
当尝试将网址添加到列表框时,它将添加整个html行而不仅仅是网址。我怎样才能添加网址?
列表框中没有显示任何内容。根据以下代码,它应该工作。并将网址添加到列表框中。
以下是用于获取链接的代码:
Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("DIV")
For Each curElement As HtmlElement In theElementCollection
If curElement.OuterHtml.Contains("active_scanner") Then
If curElement.OuterHtml.Contains("http://scanner.chad.com/SweScanner/") Then
Dim data As String = curElement.OuterHtml
data = data.Substring(data.IndexOf("http://scanner.chad.com/SweScanner/"))
If Not ListBox1.Items.Contains(data) Then ListBox1.Items.Add(data)
Label4.Text = "Added 1 URL."
End If
Else
End If
以下是网站来源:
<div class="content">
<div class="content-header"><span class="script">Active</span> Scanner</div>
<div class="section" id="active_scanner">
<ul>
<li><a href="http://scanner.chad.com/SweScanner/testing-vb">This url will be grabbed</a></li>
<li><a href="http://scanner.chad.com/SweScanner/testing-vb1">This url will be grabbed</a></li>
<li><a href="http://scanner.chad.com/SweScanner/testing-vb2">This url will be grabbed</a></li>
<li><a href="http://scanner.chad.com/SweScanner/testing-vb3-">This url will be grabbed </a></li>
<li><a href="http://scanner.chad.com/SweScanner/testing-vb4">This url will be grabbed</a></li>
<li><a href="http://scanner.chad.com/SweScanner/testing-vb5">This url will be grabbed</a></li>
<li><a href="http://scanner.chad.com/SweScanner/testing-vb6">This url will be grabbed</a></li>
<li><a href="http://scanner.chad.com/SweScanner/testing-vb7">This url will be grabbed</a></li>
<li><a href="http://scanner.chad.com/SweScanner/testing-vb8">This url will be grabbed</a></li>
<li><a href="http://scanner.chad.com/SweScanner/testing-vb9">This url will be grabbed</a></li>
<li><a href="http://scanner.chad.com/SweScanner/testing-vb10">This url will be grabbed</a></li>
<li><a href="http://scanner.chad.com/SweScanner/testing-vb11">This url will be grabbed</a></li>
</ul>
</div>
请你帮帮我。 谢谢 乍得
答案 0 :(得分:0)
试试这个
Dim i As Integer = 0
For Each Link As HtmlElement In WebBrowser1.Document.Links()
i += 1
If Link.OuterHtml.Contains("scanner.chad.com/SweScanner/testing") Then
ListBox1.Items.Add(i & ". " & Link.InnerHtml & ": " & Link.OuterHtml)
Debug.Print(i - 1 & " " & Link.OuterHtml)
End If
Next