Hi,
I'm making an Excel sheet that uses a lot of web scraping to pull the data from "td" tags within "tr". Sometimes there are more than 80 "tr" tags with 4 "td" tags in each. While it pulls it in it places the "td" tags in the same row, then goes to the next row for the next "tr" tag.
The code works fine, probably isn't close to being proper coding because I'm fairly new. My issue becomes is when IE runs into the "Automation error". Which, from what I can tell, is related to the fact that IE isn't quite ready before trying to run.
Here's the code (edited slightly to hide some personal information)
On Sheet2 it contains lists of websites that I want to pull the information from.
I've found help related to this with IE, but none of it seems to work exactly. For the Automation error is usually is around the Do...Loop that's running to wait for IE. I put in the ErrorHandling to see if it could clear it up by recreating the InternetExplorer object, but it ends up hanging after about 100 websites.
If there's anything, I'm more than appreciative for the help.
Comake
I'm making an Excel sheet that uses a lot of web scraping to pull the data from "td" tags within "tr". Sometimes there are more than 80 "tr" tags with 4 "td" tags in each. While it pulls it in it places the "td" tags in the same row, then goes to the next row for the next "tr" tag.
The code works fine, probably isn't close to being proper coding because I'm fairly new. My issue becomes is when IE runs into the "Automation error". Which, from what I can tell, is related to the fact that IE isn't quite ready before trying to run.
Here's the code (edited slightly to hide some personal information)
Code:
Sub Button1_Click()
Dim IE As New InternetExplorer
Dim shellWins As ShellWindows
Dim tr_data, td_data As IHTMLElement
Dim website_number, row_number, column_number, td_number, page_count As Integer
Set IE = CreateObject("internetexplorer.application")
website_number = 1
row_number = 1
column_number = 1
td_number = 1
For website_number = 1 To 500
IE.Visible = False
IE.navigate Sheets("Sheet2").Range("A" & website_number).Value
Do
On Error GoTo ErrorHandler
If Not IE.Busy And IE.readyState = 4 Then
Application.Wait (Now + TimeValue("00:00:01"))
If Not IE.Busy And IE.readyState = 4 Then
Exit Do
End If
End If
DoEvents
Loop
For Each tr_data In IE.document.getElementById("table").getElementsByTagName("tr")
For Each td_data In tr_data.getElementsByTagName("td")
Sheets("Sheet1").Cells(row_number, column_number).Value = Trim(td_data.innerText)
column_number = column_number + 1
td_number = td_number + 1
Next td_data
column_number = 1
td_number = 1
row_number = row_number + 1
Next tr_data
website_number = website_number + 1
Next website_number
ErrorHandler:
Set IE = Nothing
Set IE = CreateObject("internetexplorer.application")
IE.Visible = False
IE.navigate Sheets("Sheet2").Range("A" & website_number).Value
Resume Next
End Sub
On Sheet2 it contains lists of websites that I want to pull the information from.
I've found help related to this with IE, but none of it seems to work exactly. For the Automation error is usually is around the Do...Loop that's running to wait for IE. I put in the ErrorHandling to see if it could clear it up by recreating the InternetExplorer object, but it ends up hanging after about 100 websites.
If there's anything, I'm more than appreciative for the help.
Comake