我想使用getElementByClassName
从Span中提取78,但它会抛出运行时异常438。
错误438“对象不支持此属性或方法”
我有一张Excel工作表,其中包含需要在Google中搜索并提取范围值并将其粘贴到另一个Excel字段中的一组关键字。
我正在搜索的网页包含3个跨度,我需要获得第3次出现。
<a><span class="xxx">78</span></a>
VBA:
Sub ff()
Dim url As String, lastRow As Long
Dim XMLHTTP As Object, html As Object
Dim start_time As Date
Dim end_time As Date
Dim res As Object
lastRow = Range("A" & Rows.Count).End(xlUp).Row
Dim cookie As String
Dim result_cookie As String
start_time = Time
Debug.Print "start_time:" & start_time
For i = 2 To lastRow
url = "https://www.google.co.in/search?q=" & Cells(i, 1) & "&rnd=" & WorksheetFunction.RandBetween(1, 10000)
Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")
XMLHTTP.Open "GET", url, False
XMLHTTP.setRequestHeader "Content-Type", "text/xml"
XMLHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"
XMLHTTP.send
Set html = CreateObject("htmlfile")
html.body.innerHTML = XMLHTTP.ResponseText
Set res = html.getElementsByClassName("xxx")
str_text= res(3)
Cells(i,1=str_text)
DoEvents
Next
end_time = Time
Debug.Print "end_time:" & end_time
Debug.Print "done" & "Time taken : " & DateDiff("n", start_time, end_time)
MsgBox "done" & "Time taken : " & DateDiff("n", start_time, end_time)
End Sub
编辑: 即使在更改以下行后,我也会遇到运行时间438错误
Set res = html.getElementByClassName("span")(0).innerText
For el = 0 To html.getElementsByClassName("span").Length - 1
Debug.Print html.getElementsByClassName("span")(el).innerText
Next el