首页 > 解决方案 > 网站抓取:网站搜索框没有价值

问题描述

我正在尝试使用特定网站( https://icis.corp.delaware.gov/Ecorp/EntitySearch/NameSearch.aspx )交叉检查大量数据。

目标是根据 Excel 中更大的列表搜索许多公司名称,以获取其成立日期。现在我开始用一个名字来让它运行。我的主代码有问题,因为 HTML 代码中没有固有的输入值:

<input name="ctl00$ContentPlaceHolder1$frmEntityName" type="text" id="ctl00_ContentPlaceHolder1_frmEntityName" tabindex="4" size="30" maxlength="120" class="txtNormal" onkeyup="KeyEvent1(this.id)">

我尝试了以下方法:

Sub click_search()

Dim i As SHDocVw.InternetExplorer
Set i = New InternetExplorer
i.Visible = True

i.Navigate "https://icis.corp.delaware.gov/Ecorp/EntitySearch/NameSearch.aspx"

Do While i.ReadyState <> READYSTATE_COMPLETE

Loop

Dim idoc As MSHTML.HTMLDocument
Set idock = i.Document

idoc.getElementsByTagName("input").Item("ctl00$ContentPlaceHolder1$frmEntityName").Value = "10X Genomics Inc"

End Sub

我认为的问题是 HTML 代码没有固有的价值 = "" 开始,但它只出现在你写进去之后的 HTML 代码中。

我该如何解决这个问题,然后单击搜索按钮?

错误是

“对象变量或未设置块变量”

标签: htmlvbaweb-scraping

解决方案


始终在每个 VBA 代码文件的顶部使用 Option Explicit。

如果相关网页包含您感兴趣的元素的 ID,请使用 getElementById() 访问它们。此代码有效,但找不到任何记录。

Option Explicit
Sub click_search()
Dim i As SHDocVw.InternetExplorer
Dim idoc As MSHTML.HTMLDocument

Set i = New InternetExplorer
i.Visible = True
i.Navigate "https://icis.corp.delaware.gov/Ecorp/EntitySearch/NameSearch.aspx"

Do While i.ReadyState <> READYSTATE_COMPLETE
Loop

Set idoc = i.Document
idoc.getElementById("ctl00_ContentPlaceHolder1_frmEntityName").Value = "10X Genomics Inc"
idoc.getElementById("ctl00_ContentPlaceHolder1_frmFileNumber").Value = "1"
idoc.getElementById("ctl00_ContentPlaceHolder1_btnSubmit").Click
End Sub

推荐阅读