如何自动导出HTML搜索查询数据导入Excel [英] How to automate and export HTML searchable query data into excel
问题描述
我感兴趣的是从提取数据的网页上有多个搜索字段的表。我可以将数据输入这些字段,然后点击搜索按钮,在表的底部,看到基于我想搜索的信息的结果。
A webpage I am interested in extracting data from has a table with multiple search fields. I can enter data into any of these fields and click the search button at the bottom of the table and see the results based on the information I wanted to search for.
我有多个号码,我想搜索(约300),而不是搜索每一种独立,有没有一种方法可以自动搜索数据和将数据导入到Excel工作表,因为我要搜索每个号码?
I have multiple numbers i want to search for (around 300), instead of searching each of these individually, is there a way to automate searching the data and import the data into an excel worksheet for each number I want to search?
有可能使用Excel宏?
is it possible using an Excel macro?
推荐答案
您可以使用此MSXML和MSHTML库。这code应该让你开始。结果
通过运行该子同时添加引用启动(你只需要运行一次此):
You can use the MSXML and MSHTML libraries for this. This code should get you started.
Start by running this sub to add both references (you only need to run this once):
Sub addReferences()
ActiveWorkbook.VBProject.References.AddFromGuid "{3050F1C5-98B5-11CF-BB82-00AA00BDCE0B}", 4, 0
ActiveWorkbook.VBProject.References.AddFromGuid "{F5078F18-C551-11D3-89B9-0000F81FE221}", 6, 0
End Sub
然后编辑 getCAGEValues
子导入你们的笼子codeS和保存生成的数据(和你的页面需要的任何额外的数据):
Then edit the getCAGEValues
sub to import your CAGE codes and save the resulting data (and any additional data you want from the page):
Sub getCAGEValues()
Dim oHTMLDoc As MSHTML.HTMLDocument
Dim oSpan As MSHTML.HTMLGenericElement
Dim CAGECodes() As Variant
CAGECodes = Array("12345", "12346") 'CAGECodes is an array of your codes'
For Each CAGECode In CAGECodes
Set oHTMLDoc = getPage(CAGECode)
Set oSpan = oHTMLDoc.getElementById("ctl00_cphMainPageBody_lblCompNameData") 'The id for the company name'
MsgBox oSpan.innerText 'Save the value however you want to.'
Next
End Sub
Function getPage(CAGECode As Variant) As MSHTML.HTMLDocument
Dim oHttpRequest As MSXML2.XMLHTTP60
Set oHttpRequest = New MSXML2.XMLHTTP60
With oHttpRequest
.Open "GET", "http://www.logisticsinformationservice.dla.mil/BINCS/details.aspx?CAGE=" & CAGECode, False
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.send
End With
Dim oHTMLDoc As MSHTML.HTMLDocument
Set oHTMLDoc = New MSHTML.HTMLDocument
oHTMLDoc.body.innerHTML = oHttpRequest.responseText
Set getPage = oHTMLDoc
End Function
这篇关于如何自动导出HTML搜索查询数据导入Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!