VBA上的语法错误问题 [英] Syntax Error Issue on VBA

查看:54
本文介绍了VBA上的语法错误问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用VBA从网站中提取一些值

I am trying to extract some values using VBA from a Website

以下代码包含我要查找的数据数组.这样效果很好,并为我提供了我要查找的数据数组.

The following code contains the array of the data I am looking for. This works perfectly and gives me the array of the data I am looking for.

document.getElementsByClassName("list-flights")(0).querySelectorAll("[class$='price']")

以上结果为

<NodeList length="23">...</NodeList>

但是当我尝试通过使用以下代码在VBA中运行循环来提取值时

But when I am trying to extract the values by running the loop in a VBA by using the following code

lnth = document.getElementsByClassName("list-flights")(0).querySelectorAll("[class$='price']").Length - 1

For x = 0 To lnth

    firstResult = document.getElementsByClassName("list-flights")(0).querySelectorAll("[class$='price']")(x).innerText

Next x

该代码将在firstResult处给出错误,因为它不会获得任何值.我在Internet Explorer上检查了是否使用("括号,没有任何价值,但是,如果在Internet Explorer上使用"[",它可以工作,但是VBA不会接受"["括号.

The code would give error at firstResult since it would not get any value. I checked on Internet Explorer there is no value if I use "(" brackets,however if I use "[" on Internet Explorer it works but VBA would not accept "[" Brackets.

document.getElementsByClassName("list-flights")(0).querySelectorAll("[class$='price']")[0] 

以上代码可在Internet Explorer控制台上使用,但不能在VBA中使用.

The code above works on Internet Explorer Console but cant be used in VBA.

我敢肯定,如果您能提出建议解决此问题,那么我会有所帮助.

I am sure I am missing something would be of great help if you can advice what is the workaround on this.

推荐答案

要获取innerText,您可以再使用 getElementsByClassName 2次,对于每个.. next 循环,因为您的值在 list-flights->中;价格->现金js_linkInsideCell :

To get the innerText, you can use getElementsByClassName 2 more times, one of them inside for each .. next loop since your values are in list-flights -> price -> cash js_linkInsideCell:

Set priceData = IE.document.getElementsByClassName("list-flights")(0).getElementsByClassName("price")

For Each Item In priceData
    Debug.Print Item.getElementsByClassName("cash js_linkInsideCell")(0).innerHTML
Next Item

使用 .querySelectorAll("[class $ ='price']")也可以,但在解析后会导致IE对象崩溃.尝试看看它是否崩溃:

Using .querySelectorAll("[class$='price']") works as well, but it crashes IE object after parsing. Try and see if it crashes:

Set priceData = IE.document.getElementsByClassName("list-flights")(0).querySelectorAll("[class$='price']")

For Each Item In priceData
    Debug.Print Item.getElementsByClassName("cash js_linkInsideCell")(0).innerHTML
Next Item

一些简短说明:

1-您可以使用此代码等待IE对象加载网址:

1-You can use this code to wait for IE object to load url:

Do While IE.Busy
    DoEvents
Loop

2-有时,即使IE对象已准备好,查询结果也不会返回那么快.这意味着您要查找的元素尚未准备就绪并且不存在,因此您会收到错误消息:

2-Sometimes, even if IE object is ready, query results are not returned that fast. This means the elements you are looking for are not ready yet and do not exists so you get an error message:

运行时错误'91':对象变量或未设置块变量

Run-time error '91': Object variable or With block variable not set

为避免出现此消息,我的解决方法如下:

To avoid this message my workaround is as follows:

Do
On Error Resume Next
Set priceData = IE.document.getElementsByClassName("list-flights")(0).getElementsByClassName("price")
Loop Until Err.Number <> 91

我希望这会有所帮助.

这篇关于VBA上的语法错误问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆