Vba 我应该怎么做以避免出现错误消息;对象变量或未设置块变量";
我是VBA脚本的新手,已经创建了一个小脚本来从网站获取数据,但是我总是收到错误消息“Object variable或With Block variable not set”。请帮助我获得显示价格“Rp 16.425”的最佳脚本 下面是HTML和我的脚本的示例:Vba 我应该怎么做以避免出现错误消息;对象变量或未设置块变量";,vba,web-scraping,Vba,Web Scraping,我是VBA脚本的新手,已经创建了一个小脚本来从网站获取数据,但是我总是收到错误消息“Object variable或With Block variable not set”。请帮助我获得显示价格“Rp 16.425”的最佳脚本 下面是HTML和我的脚本的示例: Dim XMLPage As New MSXML2.XMLHTTP60 Dim HTMLDoc As New MSHTML.HTMLDocument Dim BHTMLDoc As New MSHTML.HTMLDocument XML
Dim XMLPage As New MSXML2.XMLHTTP60
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim BHTMLDoc As New MSHTML.HTMLDocument
XMLPage.Open "GET", "https://www.tokopedia.com/search?st=product&q=buku&ob=3&pmin=16411&origin_filter=sort_price", False
XMLPage.send
BHTMLDoc.body.innerHTML = XMLPage.responseText
Dim i As Long
For i = 0 To 3
Debug.Print HTMLDoc.querySelectorAll("[data-testid=hsRPProdPrice] span").Item(i).innerText
Next
HTML,如下屏幕所示:
<div class="S4G9wXKj">
<h3 data-testid="hSRPProdName" class="Ka_fasQS">Buku MATEMATIKA BILINGUAL Kelas X SMA/MA - Yrama Widya</h3>
<div itemprop="offers" itemtype="http://schema.org/Offer">
<span itemprop="price" class="_3fNeVBgQ">
<span data-testid="hSRPProdPrice">Rp 16.425</span>
</span>
<meta itemprop="priceCurrency" content="IDR">
<div class="ifULVzgL"><div class="_3z5GLerJ">
<span class="UY2SWg6T" data-testid="spnSRPProdTabShopLoc">Yogyakarta</span>
<span class="_1GDgKs4K">Yrama Widya Books</span></div></div>
<div class="_3dCNp1CF"></div></div></div>
此错误出现在哪一行?您应该使用
选项Explicit
启动VBA代码,以便强制声明变量。我发布了一个答案,但不确定这是否足够HtmlDoc
从未加载过数据,请使用BHtmlDoc
这是加载HTML的对象。此外,您的选择器在这一行“Debug.Print HTMLDoc.queryselectoral”([class=S4G9wXKj]div”).Item(i).innerText“对不起,martin..我是指这一行