Lecture 3: How to use VB’s webbrowser to find hyperlinks in a web page
We have already talked about how to open a web page and how to submit a web page before. Today we will take a look at how to find the URL and other elements in a web page.
1. First open VB and create a new project.
2. Right-click on the toolbar and select Components (I am using the Chinese version of VB) or select the Project menu and click Components
3. Find Microsoft Internet Controls, select it in front, and then confirm it. You will see an additional earth-like icon on the toolbar. Our implementation will all revolve around it.
4. Add a WebBrowser1 to Form1, then add a command1 to Form1, and add a list1 to form1 (this list1 should be relaxed a little, and we will put all the qualified URLs found here)
5. Add the following code to Form1: (The above are all what we have said before, so they should be familiar to you)
'----------start-----------
Private Sub Command1_Click()
Dim vTag, vDoc
Dim Allcount,i
List1.Clear
Set vDoc = WebBrowser1.Document.All
Allcount = vDoc.length
For i = 0 To Allcount - 1
If UCase(vDoc.Item(i).TagName) = "A" Then 'Find URL
vTag = vDoc.Item(i).href
If InStr(vTag, "http://dhunter.51.net") Then 'Detect whether the URL contains http://dhunter.51.net
List1.AddItem vDoc.Item(i).href 'If there is, add it to list1
End If
End If
Next i
End Sub
Private Sub Form_Load()
WebBrowser1.Navigate "http://dhunter.51.net"
End Sub
'----------Finish-----------
6. Of course, it is a test. Click the run button. We can see that our webbrowser has opened http://dhunter.51.net. After the basic download of the web page is completed, click the command1 button, and our lovely webbrowser will find the content containing The URLs of http://dhunter.51.net have been added to list1.
With these we can start to make our own simulator. Of course, more skills and more methods must be implemented by you.