【问题标题】:Web scraping: Run-time error 91 Object variable not set网页抓取:运行时错误 91 未设置对象变量
【发布时间】:2021-07-11 10:41:11
【问题描述】:

我正在尝试使用来自该网站的信息更新我的物种数据库:https://www.afcd.gov.hk/english/conservation/hkbiodiversity/database/search.php

我有一个 xlsm,其中包含 A 列中的物种列表,并在网站的搜索引擎中搜索它们中的每一个,这会导致一个页面显示指向该特定物种的另一个页面的链接。每个物种都由一个唯一的 ID 标识,这就是我想要的信息。

例如如果我在搜索框“科学名称”中输入“Mnais mneme”,则会出现一个页面,该页面显示一个包含该物种的表格,并附有其名称的链接 (https://www.afcd.gov.hk/english/conservation/hkbiodiversity/database/popup_record.php?id=781&lang=en)。 “781”是物种 ID。

我想将此链接复制到我的 xlsm 的 B 列中并在 Excel 中提取 ID:

Sub SearchBot()
 
    'dimension (declare or set aside memory for) our variables
    Dim objIE As InternetExplorer 'special object variable representing the IE browser
    
    'link will be the <a> carrying the href with the species id
    Dim link As HTMLAnchorElement
              
    'define y as interger counter
    Dim y As Integer
               
    'initiating a new instance of Internet Explorer and assigning it to objIE
    Set objIE = New InternetExplorer
 
    'make IE browser visible (False would allow IE to run in the background)
    objIE.Visible = True
 
    'navigate IE to this web page
    objIE.navigate "https://www.afcd.gov.hk/english/conservation/hkbiodiversity/database/search.php"
 
    'wait here for a few seconds while the browser is busy
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
    
    objIE.document.getElementById("s1").Click
 
    'in the search box put cell "A2" value
    objIE.document.all.Item("scientific_name").Value = Sheets("Sheet1").Range("A2").Value
 
    'click the 'Search' button
    objIE.document.getElementsByClassName("btn_3")(1).Click
    
    'wait again for the browser
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
    
    'select the species name link
    Set link = objIE.document.getElementsByTagName("td")(4).getElementsByTagName ("a")(0)
                
    y = 2
        
    'print the link to column B in Sheet1
    Sheets("Sheet1").Range("B" & y).Value = link.href
    
End Sub

调试展示

运行时错误 91

在最后一行停止时:

Sheets("Sheet1").Range("B" & y).Value = link.href

将链接设置为 HTMLAnchorElement 是否有问题?我尝试将其设置为 Object 但仍然出现错误。

【问题讨论】:

标签: html excel vba web-scraping runtime-error


【解决方案1】:

这是一些使用网络请求来查找您感兴趣的数据的代码。我查看了该页面,似乎复选框也包含SpeciesID,因此,我使用该控件的名称来查找输入的值。

代码

Public Function GetSpeciesID(ScientificName As String) As String
    Dim requestURL      As String
    Const InvalidValue  As String = "-1"
    
    'If the Scientific Name is blank, return a default value
    If (Trim$(ScientificName) = vbNullString) Then
        GetSpeciesID = InvalidValue
        Exit Function
    End If
    
    requestURL = "https://www.afcd.gov.hk/english/conservation/hkbiodiversity/database/doSearch.php?" & _
    "entity_id=0&family_name=&scientific_name=" & WorksheetFunction.EncodeURL(ScientificName) & _
    "&common_name=&chinese_name=&hk_protection_status_val=&chinared_status_val=&iucn_status_val="
    
    With CreateObject("MSXML2.ServerXMLHTTP.6.0")
        .Open "GET", requestURL
        .send
        
        Dim html As Object: Set html = CreateObject("htmlfile")
        html.body.innerhtml = .responseText
    End With
    
    GetSpeciesID = html.getElementsByName("check")(0).Value
End Function

'Run this method
Public Sub Runner()
    Debug.Print GetSpeciesID("Mnais mneme")
End Sub

【讨论】:

  • 非常感谢瑞恩!但是我遇到了一个听起来很傻的问题......在运行公共功能之前我需要做/设置什么吗?我将您的代码粘贴到一个新模块中并尝试使用 F8 运行它/调试它,但它无法运行,并且 VBE 无法将其识别为宏......
  • @Reason - 在 Ryan 的回答中看到 Sub Runner?而是从那个子调试/运行。或者您可以将其用作工作簿中的函数,因此在公式栏中它将类似于 =GetSpeciesID("Mnais mneme")
  • 感谢瑞恩和雷蒙德的一百万...它有效!!!虽然仍在尝试理解代码,但这对我来说是向前迈出的一大步 :D:D:D
猜你喜欢
相关资源
最近更新 更多
热门标签