PDA

View Full Version : Scrape data Using getElementsbyTagName NOT WORKING



kazshaz
04-24-2015, 12:46 PM
I am trying to navigate through a website by using getElementsbyTagName and then hopefully scrape data. It appears after my first trial it does not work and there is an error.
Could someone give me some advice on where I am going wrong. i've noticed T in innertext is capitalised as well if that makes a difference.
I only seem to get "one" Msgbox though and of course the beautiful Run time error '70' . Permission denied.

This is my code, you will have to change the URL to the one in the comment:

sub Scrape()
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Top = 0
ie.Left = 0
ie.Width = 800
ie.Height = 600

URL = 'kenya insurance dot gov, should be an ikr website in google
ie.Navigate URL
Do
DoEvents
If Err.Number <> 0 Then
ie.Quit
Set ie = Nothing
Goto the_start:
End If

Loop Until ie.ReadyState = 4
Dim Doc As HTMLDocument
Set Doc = ie.Document
Dim Std As String

Set AllLinks = Doc.getElementsByTagName("a")

For Each hyperlink In AllLinks

If InStr(hyperlink.href, "/index.php/acts") > 0 Then
hyperlink.Click

Exit For
End If
Next
'going on to the next website
Do
DoEvents
If Err.Number <> 0 Then
ie.Quit
Set ie = Nothing
Goto the_start:
End If
Loop Until ie.ReadyState = 4
' it is fine up till this point

Set AllLinks = Doc.getElementsByTagName("a")
For Each hyperlink In AllLinks
MsgBox hyperlink.innerText & " - " & hyperlink.href
Next
End Sub
' it loops once for the search but then I get a debugging error