__VB application of distance between cities from web crawl

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.