为什么我的使用VBA到Scrape Text的代码只能在Debug中工作 [英] Why does my code to Scrape Text using VBA works in Debug only
问题描述
我已经编写了一些代码来从Google专利网站上抓取特定日期.复习了许多示例之后,我弄清楚了getElementsByClassName可以获取所需的日期.当我逐步进入调试模式并生成所需的MsgBox时,以下代码有效.但是当我运行它时,它给我运行时错误'91':对象变量或未设置块变量."
I have written some code to scrape specific dates from Google's patent website. After reviewing lots of examples I figured out the getElementsByClassName that gets the date I need. The code below works when I step through in debug mode and generates the desired MsgBox. But when I run it, it gives me "Run-time error '91': Object variable or With block variable not set."
无论我认为这可能是一个问题,我都添加了延迟.我还取消了代码与任何与Excel电子表格的交互的关联,在这些电子表格中,我将最终放置日期,只是为了使其尽可能简单.我还将代码从原始电子表格复制到了一个新的空白电子表格,但同样存在问题.
I have added delays wherever I thought that might be an issue. I have also disassociated the code from any interaction with the Excel spreadsheet where I would ultimately put the date, just to make it as simple as possible. I've also copied the code from the original spreadsheet to a new blank one, but same issue.
任何帮助将不胜感激.
Sub Get_Date()
Dim ie As InternetExplorer
Dim sURL As String
Dim strGrant As Variant
Set ie = New InternetExplorer
sURL = "https://patents.google.com/patent/US6816842B1/en?oq=6816842"
ie.navigate sURL
ie.Visible = False
Do While ie.Busy Or ie.ReadyState < 4
DoEvents
Loop
strGrant = ie.document.getElementsByClassName("granted style-scope application-timeline")(0).innerText
Do While ie.Busy Or ie.ReadyState < 4
DoEvents
Loop
MsgBox strGrant
ie.Quit
End Sub
````
推荐答案
根据我的评论,这可能是时间安排问题.在类似问题的其他答案中也涉及到这一点.要考虑的主要事项是:
It's likely a timing issue as per my comment. That's dealt with in other answers to similar questions. Main things to consider are:
- 使用适当的页面加载等待:
IE.Busy或ie.readyState<4:DoEvents:Wend
- 可能是定时循环,尝试将元素设置为变量,然后测试是否设置.
- Use proper page load waits:
While IE.Busy Or ie.readyState < 4: DoEvents: Wend
- Possibly a timed loop to attempt to set the element to a variable then testing if set.
可替代地,有些微不足道,但似乎所有授予的日期都与发布日期(专利发布日期)相同.如果是这样,则可以使用xhr获取发布日期
Alternatively, a bit of a punt but it seems that all granted dates are the same as publication dates (patent publication date). If this is true then you can use xhr to get the publication date
Option Explicit
Public Sub GetDates()
Dim html As HTMLDocument, i As Long, patents()
patents = Array("US7724240", "US6876312", "US8259073", "US7523862", "US6816842B1")
Set html = New HTMLDocument
With CreateObject("MSXML2.XMLHTTP")
For i = LBound(patents) To UBound(patents)
.Open "GET", "https://patents.google.com/patent/" & patents(i) & "/en?oq=" & patents(i), False
.setRequestHeader "User-Agent", "Mozilla/5.0"
.send
html.body.innerHTML = .responseText
If html.querySelectorAll("[itemprop=publicationDate]").length > 0 Then
Debug.Print html.querySelector("[itemprop=publicationDate]").DateTime
End If
Next
End With
End Sub
这篇关于为什么我的使用VBA到Scrape Text的代码只能在Debug中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!