问题
I've been trying to scrap data from a WebSite, as my previous question indicates.
I was able to figure what my problem was thanks to the comunity, but now I'm facing another problem.
I don't get any error this time, but the program doesn't export any values to excel, my page still all blank.
On the other website I was scraping from, the HTML.Elements
were divs
and now it's spans
, it's because of that?
Here's my code:
Option Explicit
Public Sub Loiça()
Dim data As Object, i As Long, html As HTMLDocument, r As Long, c As Long, item As Object, div As Object
Set html = New HTMLDocument '<== VBE > Tools > References > Microsoft HTML Object Library
Dim IE As New InternetExplorer
Dim numPages As Long
numPages = GetNumberOfPages
With CreateObject("MSXML2.XMLHTTP")
' numResults = arr(UBound(arr))
' numPages = 1
For i = 1 To numPages
If i > 1 Then
.Open "GET", Replace$("https://mediamarkt.pt/pages/search-results-page?q=maquina+roupa&page=1", "page=1", "page=" & i), False
.setRequestHeader "User-Agent", "Mozilla/5.0"
.send
html.body.innerHTML = .responseText
End If
Set data = html.getElementsByClassName("snize-title")
For Each item In data
r = r + 1: c = 1
For Each div In item.getElementsByTagName("span")
With ThisWorkbook.Worksheets("Loiça")
.Cells(r, c) = div.innerText
End With
c = c + 1
Next
Next
Next
End With
'----------------------------------------------------------------------------------------------------------------------------------------------------------------------'
End Sub
Public Function GetNumberOfPages() As Long
Dim IE As New InternetExplorer
With IE
.Visible = False
.Navigate2 "https://mediamarkt.pt/pages/search-results-page?q=maquina+roupa&page=1"
While .Busy Or .readyState < 4: DoEvents: Wend
Dim numPages As Long, numResults As Long, arr() As String
arr = Split(.document.querySelector(".snize-search-results-header").innerText, Chr$(32))
numResults = arr(LBound(arr))
GetNumberOfPages = numResults
.Quit
End With
End Function
回答1:
The info is loaded dynamically. You need to use IE throughout. Also, change your css selector
Option Explicit
Public Sub WriterResults()
Dim IE As New InternetExplorer, i As Long, data As Object, span As Object, item As Object, r As Long, c As Long
With IE
.Visible = True
.Navigate2 "https://mediamarkt.pt/pages/search-results-page?q=maquina+roupa&page=1"
While .Busy Or .readyState < 4: DoEvents: Wend
Dim numPages As Long, numResults As Long, arr() As String
arr = Split(.document.querySelector(".snize-search-results-header").innerText, Chr$(32))
numResults = arr(LBound(arr))
Dim resultsPerPage As Long
resultsPerPage = .document.querySelectorAll(".snize-overhidden").Length
numPages = Application.RoundUp(numResults / resultsPerPage, 0)
For i = 1 To numPages
If i > 1 Then
.Navigate2 Replace$("https://mediamarkt.pt/pages/search-results-page?q=maquina+roupa&page=1", "page=1", "page=" & i)
While .Busy Or .readyState < 4: DoEvents: Wend
End If
Set data = .document.getElementsByClassName("snize-overhidden")
For Each item In data
r = r + 1: c = 1
For Each span In item.getElementsByTagName("span")
With ThisWorkbook.Worksheets("Loiça")
.Cells(r, c) = span.innerText
End With
c = c + 1
Next
Next
Next
.Quit
End With
End Sub
来源:https://stackoverflow.com/questions/55024184/vba-webscraping-returning-nothing-to-excel