gpt4 book ai didi

excel - VBA 网页抓取脚本返回下标超出范围

转载 作者:行者123 更新时间:2023-12-02 07:43:38 27 4
gpt4 key购买 nike

问题:

我一直在尝试从网站上抓取数据,但总是给出错误下标超出范围。我不知道为什么。我在另一个网站上使用了完全相同的代码,它运行得很好。

是的,我已经从我想要抓取的新网站更改了 div

代码:

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

Const START_URL As String = "https://mediamarkt.pt/pages/search-results-page?q=maquina+roupa&page=1"

With CreateObject("MSXML2.XMLHTTP")
.Open "GET", START_URL, False
.setRequestHeader "User-Agent", "Mozilla/5.0"
.send
html.body.innerHTML = .responseText
Dim numPages As Long, numResults As Long, arr() As String
arr = Split(html.querySelector(".snize-search-results-header").innerText, Chr$(32))
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-four-columns")
For Each item In data
r = r + 1: c = 1
For Each div In item.getElementsByTagName("div")
With ThisWorkbook.Worksheets("Loiça")
.Cells(r, c) = div.innerText
End With
c = c + 1
Next
Next
Next
End With
'----------------------------------------------------------------------------------------------------------------------------------------------------------------------'
End Sub

最佳答案

这个

html.querySelector(".snize-search-results-header").innerText

返回一个空字符串,因此当你分割时,你的arr中会得到-1。

该值可能需要 javascript 才能在页面上运行。检查返回的 html。我认为不会有任何返回。使用像selenium或IE这样的方法,允许js在页面上运行并用值更新内容

在这种情况下,您还需要 lbound,以便您可以使用返回使用 IE 的值的函数

numPages  =  GetNumberOfPages 

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

对于下一页,您将看到不同的类名称(我认为)

Set data = html.getElementsByClassName("snize-product")

检查 html 进行验证。

关于excel - VBA 网页抓取脚本返回下标超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55021575/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com