正如我先前的问题所指出的那样,我一直在尝试从Web站点抓取数据。
由于社区的原因,我能够弄清楚我的问题是什么,但是现在我面临另一个问题。
这次我没有收到任何错误,但是程序没有将任何值导出到excel,我的页面仍然全部空白。
在我抓取的另一个网站上,HTML.Elements
是divs
,现在是spans
,是因为这个原因吗?
这是我的代码:
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
答案 0 :(得分:4)
该信息是动态加载的。您需要始终使用IE。另外,更改您的CSS选择器
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