Iamlaosong Wen
A project needs the distance between the national key cities, the internet has such a query, but the speed of manual query is unbearable, so need to automatically crawl through the program. Here are some of the key points.
The URL of the query is http://tools.2345.com/jiaotong/lc.htm, which intercepts some of the source code of the page to analyze:
The most efficient function for locating elements in a Web page is getElementById, because the IDs in the Web page are unique. Through the above HTML code can be found, "origin" and "destination" can be identified by ID, in VB can be used to complete the following statements of the two content input:
WebBrowser1.Document.GetElementById ("Start"). innertext = startcity
WebBrowser1.Document.GetElementById ("End"). innertext = endcity
Where Startcity and endcity are two variables, the contents of which are city names. But the query submit button has no id attribute, so to locate this button, we must first locate this element, with function getElementsByTagName can get an element of the name of the array of all elements, and then use the subscript serial number for reference. You can determine the ordinal of a query button by using the following debugging code:
For i = 0 to WebBrowser1.Document.getElementsByTagName ("input"). Length-1
Debug.Print I & "" & WebBrowser1.Document.getElementsByTagName ("Input") (i). Value
Next I
To debug this code, the Immediate window displays the following:
0 weeks of public understanding dream
1 5823820519378234798
2 GBK
3 Search Tools
4 Hefei
5 Nanjing
6 Query
As a result, the query button's ordinal number is 6, so you can implement the query submission by using the following statement:
WebBrowser1.Document.getElementsByTagName ("Input") (6). Click
Here to illustrate, the INPUT element shown above the value of a few (0-3) in the browser to view the source code is not visible, estimated that several input should be JS program after the implementation of the production, want to see the page after the real source code, you can use the following statement to achieve:
Debug.Print WebBrowser1.Document.body.innerhtml
Once you step through this code, the Immediate window will see the full web page source code.
Finally, use the following code to achieve the crawl distance:
TIM1 = Timer
Todo
Loop while Timer < TIM1 + 1 ' Wait a moment
Todo
STR = WebBrowser1.Document.GetElementById ("res"). innertext
I1 = Len (STR)
DoEvents
If Timer > tim1 + Then Exit Do timeout exit
Loop while I1 = 0
Citydistance = Str ' distance to hand
Here's the complete code for this crawl function:
' From http://tools.2345.com/jiaotong/lc.htm Web site to catch the distance between the city public Function citydistance (startcity As String, endcity as String) as String Dim Str As String Dim i1 As Integer on Error GoTo cityerr webbrowser1.navigate "http://tools.2 345.com/jiaotong/lc.htm "TIM1 = timer Do loop while timer < TIM1 + 1" Wait a moment do Until Webbrowser1.read Ystate = 4 DoEvents Loop WebBrowser1.Document.GetElementById ("Start"). innertext = Startcity WebBrowser 1.document.getelementbyid ("End"). innertext = endcity WebBrowser1.Document.getElementsByTagName ("Input") (6). Click ' For i = 0 to WebBrowser1.Document.getElementsByTagName (' input '). Length-1 ' Debug.Print I & "" & WebBrowser1.Document.getElementsByTagName ("Input") (i). Value ' Next i tim1 = timer do loop while timer < TIM1 + 1 ' Wait a moment do Str = WebBrowser1 . document.getElementById ("res"). innertext I1 = Len (Str) DoEvents If Timer > tim1 + Then Exit Do ' timeout out loop while I1 = 0 citydistance = Str ' distance to hand exit Functi
On Cityerr: ' Debug.Print Err.Number & Err.Description err.clear Resume End Function