如何在Excel VBA中创建HTTP GET

如何在Excel VBA中创建HTTP GET,excel,vba,soap,web-scraping,Excel,Vba,Soap,Web Scraping,大家下午好 我正在做一个工作项目,我需要根据注册号和里程数获取车辆值,并将其输入Excel电子表格 注册号和里程数都存储在电子表格中,但我仍然无法确定从哪里开始 上周末我创建了一个粗略的VBA应用程序,如下所示 注册号和里程数都存储在电子表格中,但我仍然无法确定从哪里开始 上周末我创建了一个粗略的VBA应用程序,如下所示: 子GetHTMLDocument Dim IE As New SHDocVw.InternetExplorer Dim HTMLDoc As MSHTML.HTMLDocum

大家下午好

我正在做一个工作项目,我需要根据注册号和里程数获取车辆值,并将其输入Excel电子表格

注册号和里程数都存储在电子表格中,但我仍然无法确定从哪里开始

上周末我创建了一个粗略的VBA应用程序,如下所示

注册号和里程数都存储在电子表格中,但我仍然无法确定从哪里开始

上周末我创建了一个粗略的VBA应用程序,如下所示:

子GetHTMLDocument

Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim Email As MSHTML.IHTMLElement
Dim Password As MSHTML.IHTMLElement
Dim LoginButton As MSHTML.IHTMLElement
Dim REG As MSHTML.IHTMLElement
Dim Mileage As MSHTML.IHTMLElement
Dim CAPGo As MSHTML.IHTMLElement
Dim objEvent
Dim GetValue As MSHTML.IHTMLElement

'Show IE for testing purposes
IE.Visible = True
'Navigate to web page
IE.Navigate "https://valuationanywhere.cap.co.uk/LoginPage?ReturnUrl=%2f%3f__hstc%3d208265677.8bb2d3e6c872f15cd37070c17648ee29.1549763639794.1549763639794.1549763639794.1%26__hssc%3d208265677.1.1549763639794%26__hsfp%3d959865525&__hstc=208265677.8bb2d3e6c872f15cd37070c17648ee29.1549763639794.1549763639794.1549763639794.1&__hssc=208265677.1.1549763639794&__hsfp=959865525"

'Loop an empty loop until done
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop

Set HTMLDoc = IE.Document

'inputs email address
Set Email = HTMLDoc.getElementById("inputLoginEmail")
Email.Value = "email"
'inputs password
Set Password = HTMLDoc.getElementById("inputLoginPassword")
Password.Value = "password"
'Clicks login button
Set LoginButton = HTMLDoc.getElementById("btnLogin")
LoginButton.Click

'Wait 3 seconds for page to load
Application.Wait (Now + TimeValue("0:00:03"))

Set objEvent = IE.Document.createEvent("HTMLEvents")

'Input REG into text box
Set REG = HTMLDoc.getElementById("vrm")
REG.Value = "reg"
'Input mileage into text box
Set Mileage = HTMLDoc.getElementById("mileage")
Mileage.Value = "181000"

'Fakes data entry as no focus is given to the text box
objEvent.initEvent "change", False, True
REG.dispatchEvent objEvent
Mileage.dispatchEvent objEvent

'Clicks Go button
Set tags = IE.Document.getElementsByTagName("button")
For Each tagx In tags
If tagx.innerText = "Go" Then
    tagx.Click
    Exit For
End If
Next

'Wait 3 seconds for popup to load
Application.Wait (Now + TimeValue("0:00:03"))

Set tags = IE.Document.getElementsByTagName("button")
For Each tagx In tags
If tagx.innerText = "Create NEW Valuation" Then
    tagx.Click
    Exit For
End If
Next
这将导航到该页面,让我登录并搜索估值。然而,我们最终将拥有一个包含数百辆汽车的数据库,我们希望对其进行估价,我们的CAP服务在这里有一些插件-

有什么方法可以让VBA从表中选择一个注册表和里程数,并提取该值

我不指望有人能写出我想从中学到的全部东西。但是有人能给我指出正确的方向吗

最亲切的问候,
Craig

本质上,您可以将Excel中包含a列reg和B列mileage的2列范围读入2d数组,然后将数组的维度1从lbound循环到ubound,即行,并通过索引到数组中来访问reg和mileage。然后可以将这些值连接到POST请求的主体中。这是可以理解的非常高的水平,如下所示。您可以将响应读入xml文档,以便解析出所需的信息

在检索值方面,我们需要查看相关的XML

Option Explicit

Public Sub Test()
    'VBE > Tools > References > Add a reference to Microsoft HTML Object Library
    'other code

    Dim regAndMileage(), xmlDoc As Object
    Dim ws As Worksheet, r As Long, placeholderMileage As String, placeholderVR As String, body As String, response As String, html As HTMLDocument
    Const SUBSCRIBER_ID As Long = 123
    Const PASSWORD As String = "ABC"
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Set html = New HTMLDocument
    Set xmlDoc = CreateObject("MSXML2.DOMDocument")
    regAndMileage = ws.Range("A2:B4").Value      'Create the array. Reg is in col A and mileage in col B. Check datatypes when passed are as expected (int - though Long should work; and string)

    body = "<?xml version=""1.0"" encoding=""utf-8""?>"
    body = body & Chr$(10) & "<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">"
    body = body & Chr$(10) & "<soap:Body>"
    body = body & Chr$(10) & "<VRMValuation xmlns=""https://soap.cap.co.uk/vrm"">"
    body = body & Chr$(10) & "<SubscriberID>" & SUBSCRIBER_ID & " </SubscriberID>" 'int
    body = body & Chr$(10) & "<Password>" & PASSWORD & "</Password>" 'string
    body = body & Chr$(10) & "<VRM>placeholderVRM</VRM>" 'string
    body = body & Chr$(10) & "<Mileage>placeholderMileage</Mileage>" 'Mileage
    body = body & Chr$(10) & "<StandardEquipmentRequired>boolean</StandardEquipmentRequired>"
    body = body & Chr$(10) & "</VRMValuation>"
    body = body & Chr$(10) & "</soap:Body>"
    body = body & Chr$(10) & "</soap:Envelope>"

    With CreateObject("MSXML2.XMLHTTP")
        For r = LBound(regAndMileage, 1) To UBound(regAndMileage, 1)
            mileage = regAndMileage(r, 1)
            reg = regAndMileage(r, 2)
               'create your body here and concatentate in your mileage and reg variables
            .Open "POST", "protocol&domain/vrm/capvrm.asmx/VRMValuation", False
            .setRequestHeader "SOAPAction", "https://soap.cap.co.uk/vrm/VRMValuation"
            .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
            .send Replace$(Replace$(body, placeholderVRM, reg), placeholderMileage, mileage)
            response = .responseText
            With xmlDoc
                .validateOnParse = True
                .setProperty "SelectionLanguage", "XPath"
                .async = False
                If Not .LoadXML(sResponse) Then
                    Err.Raise .parseError.ErrorCode, , .parseError.reason
                End If
            End With
            'Do something to extract values
        Next
    End With
End Sub
有关范围和数组的更多信息,请参见

您可能需要在请求中添加内容长度和其他信息


你好,QHarr,谢谢你的快速回复。POST/vrm/capvrm.asmx/VRMValuation HTTP/1.1主机:soap.cap.co.uk内容类型:application/x-www-form-urlencoded内容长度:Length SubscriberID=string&Password=string&vrm=string&miliety=string&StandardEquipmentRequired=string这是在我希望能够完成所有工作的链接上?我认为只需要能够插入注册表和里程数,它返回一个结果?上面的代码只是一个概念证明,并试图重新开始编码。我的代码永远不会成为解决方案。您将在中连接值。除非有敏感信息显示,否则你能发布该帖子请求的网络标签截图吗?我很抱歉我的无知,但你所说的网络标签截图是什么意思?你在评论中从哪里提取了上述信息?我原始帖子中的链接显示了各种代码,我相信这些代码将推动这项工作。代码来自该页面底部的HTTPPOST下。还有一节是关于HTTP GET的。唯一的敏感信息是要测试的用户凭据。Regardsoh。。。。这是一个SOAP请求。您希望将变量连接到SOAP字符串中。