提交后如何从IE中提取数据

时间:2019-07-13 16:36:42

标签: excel vba

VBA的新手...

我正在尝试使用在excel中使用VBA输入提交给FFIEC地理编码网站的地址的结果代码。

我尝试使用下面的代码,但我无法得到此示例的答案8391.00

Sub fillwebform()

    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Navigate "https://geomap.ffiec.gov/FFIECGeocMap/GeocodeMap1.aspx"
    IE.Visible = True

    IE.Width = 1200
    IE.Height = 800
   'IE.FullScreen = True

    While IE.ReadyState <> 4: DoEvents: Wend

    IE.Document.getElementById("Address").Value = "121 N LaSalle St, Chicago, IL 60602"
    IE.Document.getElementById("btnSearch_label").Click

    While IE.ReadyState <> 4: DoEvents: Wend


    Dim Doc As HTMLDocument
    Set Doc = IE.Document
    Dim sDD

    sDD = Doc.getElementById("TractCode").innerText & "is the tract code"

    MsgBox sDD


End Sub

返回的代码在我的代码中只是空白

2 个答案:

答案 0 :(得分:0)

尝试此代码

Sub GetTractCode()
Dim url As String, body As String, code As String

With CreateObject("MSXML2.XMLHTTP")
    url = "https://geomap.ffiec.gov/FFIECGeocMap/GeocodeMap1.aspx/GetGeocodeData"
    body = "{""sSingleLine"": ""121 N LaSalle St, Chicago, IL 60602"", ""iCensusYear"": ""2019""}"

    .Open "POST", url, False
    .setRequestHeader "Content-Type", "application/json"
    .send body

    code = Split(.responseText, """sTractCode"":""")(1)
    code = Trim(Split(code, """,""")(0)) & " Is The Tract Code"
End With

MsgBox code
End Sub

答案 1 :(得分:0)

我建议一种更灵活的方法,该方法更快,更干净:

Option Explicit

Sub geomap()
Dim req As New WinHttpRequest
Dim jsonResponse As Object
Dim jsonRequest As Object
Dim reqBody As String
Dim searchQuery As String
Dim searchYear As String
Dim json As String
Dim url As String
url = "https://geomap.ffiec.gov/FFIECGeocMap/GeocodeMap1.aspx/GetGeocodeData"
json = "{""sSingleLine"": """", ""iCensusYear"": """"}"
searchQuery = "121 N LaSalle St, Chicago, IL 60602"
searchYear = "2019"
Set jsonRequest = JsonConverter.ParseJson(json)
jsonRequest("sSingleLine") = searchQuery
jsonRequest("iCensusYear") = searchYear
reqBody = JsonConverter.ConvertToJson(jsonRequest)
With req
    .Open "POST", url, False
    .setRequestHeader "Content-Type", "application/json; charset=utf-8"
    .send reqBody
    Set jsonResponse = JsonConverter.ParseJson(.responseText)
End With
Debug.Print jsonResponse("d")("sTractCode")
End Sub

为此,您将需要一个JSON Parser。按照链接中的说明将解析器导入您的项目。您还将需要以下参考(VB编辑器>工具>参考):

  1. Microsoft脚本运行时
  2. Microsoft WinHTTP Services版本5.1