【发布时间】:2021-05-20 03:07:46
【问题描述】:
我创建了一个脚本来解析 recipe 名称和 serving 来自这样的 links。当我在两台不同的计算机上运行相同的脚本时,我在一台计算机上得到了预期的结果,但是我遇到了这个错误运行时错误 438(对象不支持这个属性或方法) 在另一台计算机上。该脚本在下面的脚本中抛出指向这一行 Servings = .Item(I).NextSibling.innerText 的错误。
errors out的那台电脑的excel版本、操作系统和位 Excel 2019, windows 10, 64bit。
finds success的那台电脑的excel版本、操作系统和位 Excel 2013, windows 7, 32bit。
这是我使用的脚本:
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
预期输出:
Easy Tuna Casserole 8
为什么我在一台电脑上得到结果,但在另一台电脑上遇到
Run-time error 438,即使我使用相同的脚本?
【问题讨论】:
-
可能另一台PC没有Mozilla版本更新,因此您的xml解析格式不同或为空?
标签: vba web-scraping