来自USPS的刮+4

时间:2017-11-10 20:16:19

标签: excel vba excel-vba internet-explorer web-scraping

我正在尝试创建一个简单的电子表格,它将获取地址并从USPS网站查找+4压缩文件。我已经成功完成了这项工作,但结果似乎不一致。我可以运行这段代码一次,它会很好用,有时它不会返回+4。是否有更有效的写作方式?

Sub ZipCodeSearch()
Dim objIE As InternetExplorer
Dim result As String



'start IE and Navigate to USPS

    Set objIE = New InternetExplorer
    objIE.Visible = True
    objIE.navigate "https://tools.usps.com/go/ZipLookupAction_input"

   'Wait for the website to load
       Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

    'Fill in Street Address
        objIE.document.getElementById("tAddress").Value = Sheets("Sheet1").Range("A2")
    'Fill in City
        objIE.document.getElementById("tCity").Value = Sheets("Sheet1").Range("B2")


    'Fill in State-Need to figure out how to select the roller.
        'objIE.document.getElementById("select-frame").Value = Sheets("Sheet1").Range("C1")


    'Click the find Button
        objIE.document.getElementById("lookupZipFindBtn").Click

    'Wait on Search
        Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
    'Get Reslts)
        For Each aEle In objIE.document.getElementsByClassName("zip4")
            result = aEle

     'Put Results in Worksheet
             Sheets("Sheet1").Range("E2").Value = aEle.innerText
                Debug.Print aEle.innerText
        Next
     'Quit Internet Explore
     objIE.Quit

0 个答案:

没有答案