在将html结果页面定义为变量以从php页面中提取内容时访问vba炸弹

时间:2018-04-30 21:56:06

标签: vba excel-vba ms-access scrape excel

我的客户端正在尝试使用访问VBA脚本将字符串发送到我创建的php页面,然后将页面上返回的数据带回他的数据库

我使用的是mac并且无法运行任何VB代码,但这是我能够找到的(两个版本),但是在第一个昏暗的声明中都是炸弹

file1 [0]    //here button for delete this file                                                                   
file2 [1]    //here button for delete this file                                    
file3 [2]    //here button for delete this file

Private Sub Command1_Click()

Dim iHTML As HTMLDocument
Dim objHttp As MSXML2.ServerXMLHTTP
set objHttp = CreateObject("Msxml2.ServerXMLHTTP")
objHttp.open "GET", "http://www.bestenergyctri.com/zipcode.php?isValidate=adb&address1=352%20w%2046&address2=&city=new%20york&state=ny&zip5= 
", False
objHttp.send

Set iHTML = objHttp.ResponseText

straddress1 = iHTML.getElementsByClassName("detect").Item(x - 1).getElementsByClassName("thedata").Item(0).getElementsByClassName("address1").Item(1).innerText
straddress2 = iHTML.getElementsByClassName("detect").Item(x - 1).getElementsByClassName("thedata").Item(0).getElementsByClassName("address2").Item(1).innerText
strcity = iHTML.getElementsByClassName("detect").Item(x - 1).getElementsByClassName("thedata").Item(0).getElementsByClassName("City").Item(1).innerText
strstate = iHTML.getElementsByClassName("detect").Item(x - 1).getElementsByClassName("thedata").Item(0).getElementsByClassName("State").Item(1).innerText
strzip5 = iHTML.getElementsByClassName("detect").Item(x - 1).getElementsByClassName("thedata").Item(0).getElementsByClassName("Zip5").Item(1).innerText
strzip4 = iHTML.getElementsByClassName("detect").Item(x - 1).getElementsByClassName("thedata").Item(0).getElementsByClassName("Zip4").Item(1).innerText

SaveWebInfo straddress1, straddress2, strcity, strstate, strzip5, strzip4
Set iHTML = Nothing
Set objHttp = Nothing

End Sub

有没有人对如何让页面正确读入变量有任何建议,以便可以解析它

感谢

1 个答案:

答案 0 :(得分:0)

这是一个使用CSS querySelector并避免使用匈牙利表示法的简单示例

<强>代码:

Option Explicit

Sub test()

    Dim iHTML As New HTMLDocument, objHttp As MSXML2.ServerXMLHTTP60 '<== Note this is version specific syntax. 60 is for Excel 2016
    Set objHttp = New MSXML2.ServerXMLHTTP60
    objHttp.Open "GET", "http://www.bestenergyctri.com/zipcode.php?isValidate=adb&address1=352%20w%2046&address2=&city=new%20york&state=ny&zip5=", False
    objHttp.send

    Dim address1  As String, address2 As String, city As String, state As String, zip5 As String, zip4 As String
    With iHTML
        .body.innerHTML = objHttp.ResponseText
        address1 = .querySelector(".address1").innerText
        address2 = .querySelector(".address2").innerText
        city = .querySelector(".City").innerText
        state = .querySelector(".State").innerText
        zip5 = .querySelector(".Zip5").innerText
        zip4 = .querySelector(".Zip4").innerText
    End With

    Debug.Print "Address 1: " & address1
    Debug.Print "Address 2: " & address2
    Debug.Print "City: " & city
    Debug.Print "State: " & state
    Debug.Print "Zip5: " & zip5
    Debug.Print "Zip4: " & zip4
End Sub

<强>输出:

Example output

引用添加到VBA&gt;工具&gt;参考文献:

References

*最后两个参考文献是重要的参考文献。