问题描述
我创建了一个脚本来解析 recipe
名称和 serving
来自这样的 链接.当我在两台不同的计算机上运行相同的脚本时,我在一台计算机上得到了预期的结果,但我遇到了这个错误运行时错误 438(对象不支持此属性或方法) 在另一台计算机中.脚本抛出该错误,指向下面脚本中的这一行 Servings = .Item(I).NextSibling.innerText
.
出错
的那台电脑的 Excel 版本、操作系统和位 Excel 2019、windows 10、64 位
.
找到成功
的那台电脑的excel版本、操作系统和位 Excel 2013、windows 7、32位
.
这是我使用的脚本:
公共子 FetchRecipeInfo()Dim Http As Object、Html As HTMLDocument、Servings$、R&Dim Url As Variant, linkList As Variant, I&, Ws As Worksheet设置 Html = 新建 HTMLDocument设置 Http = CreateObject("MSXML2.XMLHTTP")Set Ws = ThisWorkbook.Worksheets(Sheet1")R = 1链接列表 = 数组( _https://www.allrecipes.com/recipe/18871/easy-tuna-casserole/",_https://www.allrecipes.com/recipe/21791/tuna-noodle-casserole-iv/"_)对于链接列表中的每个 URL使用 Http.打开GET",网址,假.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36".发送Html.body.innerHTML = .responseText结束于Ws.Cells(R, 1) = Html.querySelector(h1.headline").innerText使用 Html.querySelectorAll(".recipe-meta-item-header")对于 I = 0 到 .Length - 1如果 InStr(.item(I).innerText, "Servings:") >0 那么份数 = .item(I).NextSibling.innerTextWs.Cells(R, 2) = 服务退出万一接下来我结束于R = R + 1下一个网址结束子
预期输出:
简易金枪鱼砂锅 8
为什么我在一台电脑上得到结果,但在另一台电脑上遇到运行时错误 438
,即使我使用相同的脚本?
我不知道为什么它可以在另一台机器上运行.我假设与底层解析器有关.使用我的设置(Microsoft® Excel® 2019 MSO (16.0.13929.20206) 32 位 Windows 10 MSHTML.dll 11.00.19041.985),有一个文本节点作为直接相邻的兄弟节点,它没有 .innerText
属性,但您可以使用 .nodeValue
.相反,我认为您想继续学习下一个兄弟姐妹,即
Servings = .Item(I).NextSibling.NextSibling.innerText
这适用于 Windows 10 64 位.因此,如果此新行在您的 Win 7 上失败,则根据版本包含在 If Else 中.
I've created a script to parse recipe
name and serving
from such links. When I run the same script in two different computers, I get expected results in one computer but I encounter this error Run-time error 438 (Object doesn't support this property or method) in another computer. The script throws that error pointing at this line Servings = .Item(I).NextSibling.innerText
within the script below.
The excel version, OS and bit Excel 2019, windows 10, 64bit
of that pc which errors out
.
The excel version, OS and bit Excel 2013, windows 7, 32bit
of that pc which finds success
.
This is the script that I used :
Public Sub FetchRecipeInfo()
Dim Http As Object, Html As HTMLDocument, Servings$, R&
Dim Url As Variant, linkList As Variant, I&, Ws As Worksheet
Set Html = New HTMLDocument
Set Http = CreateObject("MSXML2.XMLHTTP")
Set Ws = ThisWorkbook.Worksheets("Sheet1")
R = 1
linkList = Array( _
"https://www.allrecipes.com/recipe/18871/easy-tuna-casserole/", _
"https://www.allrecipes.com/recipe/21791/tuna-noodle-casserole-iv/" _
)
For Each Url In linkList
With Http
.Open "GET", Url, False
.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
.send
Html.body.innerHTML = .responseText
End With
Ws.Cells(R, 1) = Html.querySelector("h1.headline").innerText
With Html.querySelectorAll(".recipe-meta-item-header")
For I = 0 To .Length - 1
If InStr(.item(I).innerText, "Servings:") > 0 Then
Servings = .item(I).NextSibling.innerText
Ws.Cells(R, 2) = Servings
Exit For
End If
Next I
End With
R = R + 1
Next Url
End Sub
Expected output:
Easy Tuna Casserole 8
I don't know why it works on another machine. I assume something to do with the underlying parsers. With my set-up (Microsoft® Excel® 2019 MSO (16.0.13929.20206) 32-bit Windows 10 MSHTML.dll 11.00.19041.985), there is a text node as immediate adjacent sibling which doesn't have a .innerText
property, but rather you could use .nodeValue
. Instead, I think you wanted to move on to the next sibling i.e.
Servings = .Item(I).NextSibling.NextSibling.innerText
That works on Windows 10 64 bit. So, if this new line fails on your Win 7 then wrap in an If Else based on version.
这篇关于在一台电脑上得到了结果,但在另一台电脑上遇到了“运行时错误 438",即使我使用了相同的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!