Jacob45678
New Member
- Joined
- Sep 17, 2022
- Messages
- 10
- Office Version
- 2019
- Platform
- Windows
Hi, I want to web scrape information to an excel worksheet for multiple pages. example from page 1 to 3. I tried a code found online from googling but it sends out an error. Any help is much appreciated. Thanks in advance.
I would like to sort the information like the example shown below.
The Website
The Code (VBA)
Sub TableScrapeFromURL()
'
Dim ArrayColumn As Long, ArrayRow As Long
Dim TableCell As Object, TableRow As Object
Dim Doc As Object, http As Object
Dim ScrapedTableArray As Variant
'
Set Doc = CreateObject("htmlFile") ' Late bind Microsoft HTML Object Library ... This can
' ' be late bound as long as 'class' is not needed
Set http = CreateObject("msxml2.xmlhttp") ' Late bind Microsoft XML
'
ArrayColumn = 1: ArrayRow = 1 ' Initialize ArrayColumn & ArrayRow to 1
'
With http '
.Open "GET", "Steam Community Market :: Showing all listings", False ' Load website to load
.Send ' Send website to load
Doc.body.innerHtml = .responseText ' save all data received from website
End With
'
With Doc.getelementsbytagname("table")(0) '
ReDim ScrapedTableArray(1 To .Rows.Length, 1 To .Rows(1).Cells.Length) ' Resize ScrapedTableArray
'
For Each TableRow In .Rows ' Loop through each TableRow
For Each TableCell In TableRow.Cells ' Loop through each TableCell in TableRow
ScrapedTableArray(ArrayRow, ArrayColumn) = TableCell.innerText ' Save value into ScrapedTableArray
ArrayColumn = ArrayColumn + 1 ' Increment ArrayColumn
Next ' Loop back
'
ArrayColumn = 1: ArrayRow = ArrayRow + 1 ' Reset ArrayColumn to 1, Increment ArrayRow
Next ' Loop back
End With
'
Range("A1").Resize(UBound(ScrapedTableArray, 1), _
UBound(ScrapedTableArray, 2)) = ScrapedTableArray ' Display ScrapedTableArray to sheet
End Sub
I would like to sort the information like the example shown below.
The Website
Steam Community Market :: Showing all listings
steamcommunity.com
The Code (VBA)
Sub TableScrapeFromURL()
'
Dim ArrayColumn As Long, ArrayRow As Long
Dim TableCell As Object, TableRow As Object
Dim Doc As Object, http As Object
Dim ScrapedTableArray As Variant
'
Set Doc = CreateObject("htmlFile") ' Late bind Microsoft HTML Object Library ... This can
' ' be late bound as long as 'class' is not needed
Set http = CreateObject("msxml2.xmlhttp") ' Late bind Microsoft XML
'
ArrayColumn = 1: ArrayRow = 1 ' Initialize ArrayColumn & ArrayRow to 1
'
With http '
.Open "GET", "Steam Community Market :: Showing all listings", False ' Load website to load
.Send ' Send website to load
Doc.body.innerHtml = .responseText ' save all data received from website
End With
'
With Doc.getelementsbytagname("table")(0) '
ReDim ScrapedTableArray(1 To .Rows.Length, 1 To .Rows(1).Cells.Length) ' Resize ScrapedTableArray
'
For Each TableRow In .Rows ' Loop through each TableRow
For Each TableCell In TableRow.Cells ' Loop through each TableCell in TableRow
ScrapedTableArray(ArrayRow, ArrayColumn) = TableCell.innerText ' Save value into ScrapedTableArray
ArrayColumn = ArrayColumn + 1 ' Increment ArrayColumn
Next ' Loop back
'
ArrayColumn = 1: ArrayRow = ArrayRow + 1 ' Reset ArrayColumn to 1, Increment ArrayRow
Next ' Loop back
End With
'
Range("A1").Resize(UBound(ScrapedTableArray, 1), _
UBound(ScrapedTableArray, 2)) = ScrapedTableArray ' Display ScrapedTableArray to sheet
End Sub