Link to home
Start Free TrialLog in
Avatar of francislam
francislam

asked on

Get External Web Result into Excel

I am working on an Excel model which need to look for the state, city and county name from an external web site.

Taking the web site "www.geonames.org" for example.  Is it possible for me to input the zip code and country in two cells in Excel and then it will return the searched result of "Name" and "Country" back to Excel?

Francis
Avatar of Nico Bontenbal
Nico Bontenbal
Flag of Netherlands image

Yes you can. See the attached example. It uses the wininet api to retrieve the result from the website. Then it searches the right information inside the result. The function GetInternetResult is used to get the information from the website. The function GeoNames sends the proper request to the website and filters out the Place from the results.
When you enter a country and postal on rows 2 or 3 the place will show up in column C.

Geo.xlsm
Yes, that is possible.

 web-query.xlsm
Sure this is possible, there are really 2 approaches:

1. Use a web service.
2. Use some form of IE automation to request the page, parse the html returned to extract your results.

The first approach is the best as it is specifically designed to do what you want but is reliant on the website having a web service available for you to use. The second is no more difficult to impliment but can be easily broken if the website updates their web pages.

See: http://www.excely.com/excel-vba/ie-automation.shtml
Avatar of francislam
francislam

ASKER

Hi Nicobo,

Is it possible to add one more function GeoCountry to show the country details in column D, i.e., the wordings including "United States, California, Los Angeles County"?

   User generated image
SOLUTION
Avatar of Nico Bontenbal
Nico Bontenbal
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi akoster,

Is it possible just to show the Name and Country in the result area?  I also don't need the words "wikipedia article" and those strange characters "Bev"rli Khils,Beverly Hills,beverli hilsi,bibarihiruzu...etc.

Thanks

 User generated image
Hi Nicobo,

If I input "Las Vegas", it seems it cannot show "Clark County" properly before the "/".

 User generated image
I assumed you wanted to input country and zipcode. "Las Vegas" is not a zipcode.

Your question was about pulling in data from a website. I uploaded a code example that demonstrates a technique to do this. Now it is up to you to adapt this code to your specific needs.
Hi Nicobo,

I understand "Las Vegas" is not a zipcode but the point is even if I enter "89101" as the zip code, it still cannot show "Clark County" properly as showed below:

 User generated image
 
 User generated image
Sure, you could link the cell representing the country and the cell representing the name.

The problem with this web page is that the results for different searches are not represented in the same way.
For some (US) results, the country is placed in cell C6 while for other searches is resides in G5.

So to be safe, i'd suggest parsing the web results with a macro, more or less as Nicobo does.
It could be performed however with less and more understandable code like this :

 web-query2.xlsm
I'm using this page to get the result:
http://www.geonames.org/postalcode-search.html?q=89101&country=US
because it has a clearer structure and thus is easier to process.
I saw the site has a webservice also:
http://www.geonames.org/export/web-services.html
The information retrieved from those pages is probably even easier to process.
or more reliable :
Sub websearch()
Dim ie As Object

    Application.StatusBar = "Initialising"
    Set ie = CreateObject("internetexplorer.application")
        
    Application.StatusBar = "Processing web query"
    ie.navigate "http://www.geonames.org/search.html?q=" & Range("B1") & "&country=" & Range("B2")
    
    Do While ie.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop
       
    Application.StatusBar = "Parsing results"
    For Each element In ie.document.getelementsbytagname("table")
        If element.classname = "restable" Then
            pos_name = -1
            pos_countru = -1
            
            '-- determine result fields
            For x = 0 To element.Children.Length - 1
                For y = 0 To element.Children(x).Children.Length - 1
                    For Z = 0 To element.Children(x).Children(y).Children.Length - 1
                        Debug.Print x & "." & y & "." & Z & " => " & element.Children(x).Children(y).Children(Z).innerhtml
                        If element.Children(x).Children(y).Children(Z).innertext = "Name" Or element.Children(x).Children(y).Children(Z).innertext = "Place" Then
                            name_x = x
                            name_y = y
                            name_z = Z
                        ElseIf element.Children(x).Children(y).Children(Z).innertext = "Country" Then
                            country_x = x
                            country_y = y
                            country_z = Z
                        End If
                    Next Z
                Next y
                If name_x > 0 And country_x > 0 Then Exit For
            Next x
                       
            '-- determine result values
            If element.Children(name_x).Children(name_y + 1).Children(name_z).Children.Length > 0 Then
                Range("B4") = element.Children(name_x).Children(name_y + 1).Children(name_z).FirstChild.innertext
            Else
                Range("B4") = element.Children(name_x).Children(name_y + 1).Children(name_z).innertext
            End If
            
            If element.Children(country_x).Children(country_y + 1).Children(country_z).Children.Length > 0 Then
                Range("B5") = element.Children(country_x).Children(country_y + 1).Children(country_z).FirstChild.innertext
            Else
                Range("B5") = element.Children(country_x).Children(country_y + 1).Children(country_z).innertext
            End If
        End If
    Next element
    If Not ie.Visible Then
        ie.Quit
        Set ie = Nothing
    End If
End Sub

Open in new window

Hi Nicobo,

I see and I guess this may be the problem as I can only see "Clark" under Admin 2 at the postalcode-serach.html.  Seem the main page does provide a more complete information than the postalcode search page.

 User generated image
Hi akoster,

Any way to show also "Nevada" and "Clark County" in your search result? Either in one cell or in different cells or rows will be fine.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As Nicobo is the first one who suggest parsing the web results with a macro, I have allocated 100 points to him.  Hope akoster is OK with this.
excellent !