我已经在vba中编写了一个脚本来从网页中获取一些集合名称,并且脚本会相应地获取它们,直到在执行过程中发现某个错误为止。这是我第一次遇到这样的错误。
我的脚本正在执行的工作是获取
Company Sets
下的所有链接,然后向下追踪每个链接,使其深入一层,然后跟随Set Name
下的所有链接,对其进行深入另一层整理,最终从那里解析表。我解析了PUBLISHED SET
的名称,该名称存储在变量bName
中,而不是存储在表中,因为脚本越来越大。我使用IE来获取PUBLISHED SET
,因为几乎没有导致编码问题的线索。
我搜索了所有地方,找到了解决方法,但是没有运气。
但是,我遇到了 this thread ,其中有一个用vb编写的建议解决方案,但无法弄清楚如何使其在vba中工作
我正在尝试的脚本:
Sub FetchRecords()
Const baseUrl$ = "https://www.psacard.com"
Const link = "https://www.psacard.com/psasetregistry/baseball/company-sets/16"
Dim IE As New InternetExplorer, Htmldoc As HTMLDocument
Dim Http As New XMLHTTP60, Html As New HTMLDocument, bName$, tRow As Object
Dim post As Object, elem As Object, posts As Object, I&, R&, C&
Dim key As Variant
Dim idic As Object: Set idic = CreateObject("Scripting.Dictionary")
With Http
.Open "GET", link, False
.send
Html.body.innerHTML = .responseText
End With
Set posts = Html.querySelectorAll(".dataTable tr td a[href*='/psasetregistry/baseball/company-sets/']")
For I = 0 To posts.Length - 7
idic(baseUrl & Split(posts(I).getAttribute("href"), "about:")(1)) = 1
Next I
For Each key In idic.Keys
With Http
.Open "GET", key, False
.send
Html.body.innerHTML = .responseText
End With
For Each post In Html.getElementsByTagName("a")
If InStr(post.getAttribute("title"), "Contact User") > 0 Then
If InStr(post.ParentNode.getElementsByTagName("a")(0).getAttribute("href"), "publishedset") > 0 Then
IE.Visible = True
IE.navigate baseUrl & Split(post.ParentNode.getElementsByTagName("a")(0).getAttribute("href"), "about:")(1)
While IE.Busy = True Or IE.readyState < 4: DoEvents: Wend
Set Htmldoc = IE.document
bName = Htmldoc.querySelector("h1 b.text-primary").innerText
If InStr(bName, "/") > 0 Then bName = Split(Htmldoc.querySelector(".inline-block a[href*='/contactuser/']").innerText, " ")(1)
R = R + 1: Cells(R, 1) = bName
End If
End If
Next post
Next key
IE.Quit
End Sub
在提取70到90之间的记录后,我得到了指向以下行的错误:
bName = Htmldoc.querySelector("h1 b.text-primary").innerText
错误看起来像:
Automation Error: old format or invalid type library
用vb编写的链接线程中的建议解决方案(无法转换为vba):
'save the current settings for easier restoration later
Dim oldCI As System.Globalization.CultureInfo = _
System.Threading.Thread.CurrentThread.CurrentCulture
'change the settings
System.Threading.Thread.CurrentThread.CurrentCulture = _
New System.Globalization.CultureInfo("en-US")
Your code here
'restore the previous settings
System.Threading.Thread.CurrentThread.CurrentCulture = oldCI