Getting info from HTML

Mattestion

New Member
Joined
May 22, 2011
Messages
19
I'm doing a reverse address search on Canada411 and then trying to get all the names and phone numbers on the results page into excel. I have no problem navigating to the site and submitting my info. It's trying to get the names I can't figure out. I see the name is in a link but I don't know how to distinguish that link from the others (no name,value,etc. attribute).

Here is my code so far:
Code:
Sub TelephonePull()
Dim ie As Object
Dim doc As Object

 Set ie = CreateObject("internetexplorer.application")
    ie.Visible = True
    ie.navigate "http://www.canada411.ca/?old=no"  'This is so we go to the new version of Canada411
    
    Do
        If ie.readyState = 4 Then
            Exit Do
        Else
            DoEvents
        End If
    Loop
    
    ie.navigate "http://www.canada411.ca/search/address.html"
 
    Do
        If ie.readyState = 4 Then
            Exit Do
        Else
            DoEvents
        End If
    Loop
    
Set doc = ie.document
    doc.getelementbyid("st").Value = "85 BRITTANY"
    doc.getelementbyid("ci").Value = "MONT-ROYAL"
    doc.getelementbyid("pv").Value = "QC"
    doc.getelementbyid("c411AddressFind").Click
    
    Do
        If ie.readyState = 4 Then
            Exit Do
        Else
            DoEvents
        End If
    Loop
    
    Do While doc.readyState <> "complete": DoEvents: Loop
    
Set doc = ie.document
    doc.getelementbyid("c411PerPage").Value = "50"
    doc.getelementbyid("c411PerPage").FireEvent ("onchange")
    
    Do
        If ie.readyState = 4 Then
            Exit Do
        Else
            DoEvents
        End If
    Loop
    
    Do While doc.readyState <> "complete": DoEvents: Loop
    
    'Code for getting names and phone numbers on results page
    
End Sub
 
Last edited:

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
What exactly is the problem?

Can't you just use innerText to get the data from the DIV element the listing is in?

The DIV element can be identified by it's classname.

Something like this perhaps.
Code:
    Set doc = ie.document
    ' get collection of all DIV elements on page
    
    Set colDivs = doc.getelementsbytagname("DIV")
 
    Set rng = Range("A1")
    
    ' loop through divs
    For Each DetailsDiv In colDivs
 
        ' if div is the one we want get the data and put it on a worksheet
        If DetailsDiv.classname = "listingDetail" Then
 
            For Each elm In DetailsDiv.all

                Select Case elm.tagname
                    Case "UL", "DIV"
                        rng.Value = elm.innertext
                        Set rng = rng.Offset(, 1)
                End Select

            Next elm
                        
            Set rng = Cells(rng.Row + 1, 1)
            
        End If

    Next DetailsDiv
That's pretty simplistic, and looping through all the DIV elements is kind of brute force but apart from the page load it seems quite fast.

The data 'extraction' bit is particularly simplistic, just grabs the text from the type of elements that seem relevant.

If you look closer at the page/HTML, you might be able to refine it.
 
Upvote 0

Forum statistics

Threads
1,224,588
Messages
6,179,743
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top