Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub GetHTMLDocument()
- 'dimension (set aside memory for) our variables
- Dim objIE As InternetExplorer
- Dim ele As Object
- Dim y As Integer
- 'start a new browser instance
- Set objIE = New InternetExplorer
- 'make browser visible
- objIE.Visible = True
- 'navigate to page with needed data
- objIE.navigate "http://mlb.mlb.com/stats/sortable.jsp#elem=%5Bobject+Object%5D&tab_level=child&click_text=Sortable+Player+hitting&game_type='R'&season=2018&season_type=ANY&league_code='MLB'§ionType=sp&statType=hitting&page=1&ts=1526432697176"
- 'wait for page to load
- Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
- 'we will output data to excel, starting on row 1
- y = 0
- 'look at all the 'tr' elements in the 'table' with id 'myTable',
- 'and evaluate each, one at a time, using 'ele' variable
- For Each ele In objIE.document.getElementById("datagrid"). _
- getElementsByTagName("tr")
- 'show the text content of 'tr' element being looked at
- Debug.Print ele.textContent
- 'each 'tr' (table row) element contains 4 children ('td') elements
- 'put text of 1st 'td' in col A
- Sheets("Sheet1").Range("A" & y).Value = ele.Children(0).textContent
- 'put text of 2nd 'td' in col B
- Sheets("Sheet1").Range("B" & y).Value = ele.Children(1).textContent
- 'put text of 3rd 'td' in col C
- Sheets("Sheet1").Range("C" & y).Value = ele.Children(2).textContent
- 'put text of 4th 'td' in col D
- Sheets("Sheet1").Range("D" & y).Value = ele.Children(5).textContent
- 'put text of 4th 'td' in col f
- Sheets("Sheet1").Range("E" & y).Value = ele.Children(22).textContent
- 'increment row counter by 1
- y = y + 1
- Next
- 'save the Excel workbook
- ActiveWorkbook.Save
- End Sub
- Sub GetHTMLDocument()
- Application.ScreenUpdating = False
- 'dimension (set aside memory for) our variables
- Dim objIE As InternetExplorer
- Dim ele As Object
- Dim y As Integer
- Dim EstaPagina As Byte
- Dim EstaURL As String
- 'Página inicial
- EstaPagina = 1
- 'we will output data to excel, starting on row 1
- y = 1
- EstaURL = "http://mlb.mlb.com/stats/sortable.jsp#elem=%5Bobject+Object%5D&tab_level=child&click_text=Sortable+Player+hitting&game_type='R'&season=2018&season_type=ANY&league_code='MLB'§ionType=sp&statType=hitting&page=" '&ts=1526432697176"
- 'start a new browser instance
- Set objIE = New InternetExplorer
- 'make browser visible
- objIE.Visible = False
- Do Until EstaPagina = 255
- 'navigate to page with needed data
- objIE.navigate EstaURL & EstaPagina
- 'wait for page to load
- Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
- 'If UCase(Left(EstaURL, 211) & "1") = UCase(Left(objIE.LocationURL, (211 + Len(EstaPagina)))) And y > 1 Then Exit Do
- 'look at all the 'tr' elements in the 'table' with id 'myTable',
- 'and evaluate each, one at a time, using 'ele' variable
- For Each ele In objIE.document.getElementById("datagrid").getElementsByTagName("tr")
- 'show the text content of 'tr' element being looked at
- 'Debug.Print ele.textContent
- 'each 'tr' (table row) element contains 4 children ('td') elements
- 'put text of 1st 'td' in col A
- Sheets("Sheet1").Range("A" & y).Value = ele.Children(0).textContent
- 'put text of 2nd 'td' in col B
- Sheets("Sheet1").Range("B" & y).Value = ele.Children(1).textContent
- 'put text of 3rd 'td' in col C
- Sheets("Sheet1").Range("C" & y).Value = ele.Children(2).textContent
- 'put text of 4th 'td' in col D
- Sheets("Sheet1").Range("D" & y).Value = ele.Children(5).textContent
- 'put text of 4th 'td' in col f
- Sheets("Sheet1").Range("E" & y).Value = ele.Children(22).textContent
- 'increment row counter by 1
- y = y + 1
- Next
- EstaPagina = EstaPagina + 1
- Loop
- objIE.Quit
- Set objIE = Nothing
- Set ele = Nothing
- Range("A1").Select
- Range(Selection, Selection.End(xlToRight)).Select
- Range(Selection, Selection.End(xlDown)).Select
- Selection.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5), _
- Header:=xlNo
- Application.ScreenUpdating = True
- MsgBox "Volcado terminado", vbInformation
- Range("A1").Select
- 'save the Excel workbook
- ActiveWorkbook.Save
- End Sub
Add Comment
Please, Sign In to add comment