0

我正在处理一个以前处理过的问题,但不是在这种情况下。

我正在使用 VBA 从 USPS 网站提取地址。当我放入单元格“ele.innertext”时,我会得到类中的所有内部文本,但 VBA 不会让我将内部文本隔离到项目级别 - 例如,ele.item(1).innertext 给我上述错误。你知道为什么吗?

我的浏览器是IE11。

相关HTML:

<div id="zipByAddressDiv" class="industry-detail">Loading...</div>

                            <!-- start Handlebars template -->
                            <script id="zipByAddressTemplate" type="text/x-handlebars-template">
                                <ul class="list-group industry-detail">
                                {{#each addressList}}
                                    <li class="list-group-item paginate">
                                        <div class="zipcode-result-address">
                                            <p>{{companyName}}</p>
                                            <p>{{addressLine1}}</p>
                                            <p>{{city}} {{state}} <strong>{{zip5}}-{{zip4}}</strong></p>

VBA:

   Sub USPS()

Dim eRow As Long
Dim ele As Object
Dim objie As Object
Dim wscript As Object
Dim test As String
Dim testarray() As String
'Dim goods As Object
Dim r As Integer
Dim x As Long: x = 0
Dim vFacility As Variant
Dim y As Variant
'Dim IE As New InternetExplorer
Sheets("Address").Select

eRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Set objie = CreateObject("InternetExplorer.Application")

For r = 4 To 8

myaddress = Cells(r, 5).Value
mycity = Cells(r, 7).Value
mystate = Cells(r, 8).Value
myzipcode = Cells(r, 9).Value

'myaddress = Range("a2").Value
'mycity = Range("c2").Value
'mystate = Range("d2").Value
'myzipcode = Range("e2").Value


With objie
.Visible = True
.navigate "https://tools.usps.com/go/ZipLookupAction!input.action"

Do While .Busy
DoEvents
Loop



Set what = .document.getElementsByName("tAddress")
what.Item(0).Value = myaddress
Set zipcode = .document.getElementsByName("tCity")
zipcode.Item(0).Value = mycity
Set zipcode1 = .document.getElementsByName("tState")
zipcode1.Item(0).Value = mystate
Set zipcode2 = .document.getElementsByName("tZip-byaddress")
zipcode2.Item(0).Value = myzipcode

.document.getElementByID("zip-by-address").Click


Do While .Busy
DoEvents
Loop


 For Each ele In .document.all

Select Case ele.className
Case "industry-detail"
test = ele.innertext
testarray = Split(test, vbCrLf)

Worksheets("Address").Cells(r, 11).Value = testarray(4)

'Debug.Print test
'Debug.Print "and"
'Debug.Print testarray(4)

End Select

Next ele
End With



Next r
Set objie = Nothing
Set ele = Nothing
Set IE = Nothing

'IE.Quit


End Sub
4

1 回答 1

1

我认为您正在尝试做的是输入地址详细信息并检索找到的邮政编码。此方法使用CSS 选择器来定位页面样式,我立即从地址搜索 URL 开始。我尽可能使用 id 选择器(这与说 相同.document.getElementById("yourID"),表示#为这些是最快的检索方法。在选择状态时,这是一个下拉列表,我选择适当的选项。您可以连接搜索状态 2 个字母选项字符串的缩写,例如

Dim state As String 
state = "NY"
.querySelector("option[value=" & state &  "]").Selected = True

有一个循环来确保目标元素出现在新的搜索结果页面中。我使用另一个 CSS 选择器#zipByAddressDiv strong来仅定位结果中以粗体显示的邮政编码。粗体由strong标签设置。

strong结果中包含邮政编码的标签:

在此处输入图像描述

CSS查询:

在此处输入图像描述

上面的 CSS 选择器是使用 id 来定位的#zipByAddressDiv,然后,它不是拆分为一个数组来获取所需的值,而是使用后代选择器来定位strong包含所需值的标记元素。


VBA:

Option Explicit
Public Sub AddressSearch()
    Dim IE As New InternetExplorer, t As Date, ele As Object
    Const MAX_WAIT_SEC As Long = 5

    With IE
        .Visible = True
        .navigate "https://tools.usps.com/zip-code-lookup.htm?byaddress"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document
            .querySelector("#tAddress").Value = "1 Main Street"
            .querySelector("#tCity").Value = "New York"
            .querySelector("option[value=NY]").Selected = True
            '  .querySelector("#tZip-byaddress").Value = 10045
            .querySelector("#zip-by-address").Click
        End With

        While .Busy Or .readyState < 4: DoEvents: Wend

        t = Timer
        Do
            DoEvents
            On Error Resume Next
            Set ele = .document.querySelector("#zipByAddressDiv strong")
            On Error GoTo 0
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop While ele Is Nothing

        Debug.Print ele.innerText
        .Quit
    End With
End Sub

这是循环中的样子:

Option Explicit
Public Sub AddressSearch()
    Dim IE As New InternetExplorer, t As Date, ele As Object, i As Long
    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Address")
    Const MAX_WAIT_SEC As Long = 5

    With IE
        .Visible = True

        For i = 4 To 8

            .navigate "https://tools.usps.com/zip-code-lookup.htm?byaddress"

            While .Busy Or .readyState < 4: DoEvents: Wend

            With .document
                .querySelector("#tAddress").Value = ws.Cells(i, 5).Value
                .querySelector("#tCity").Value = ws.Cells(i, 7).Value
                .querySelector("option[value=" & ws.Cells(i, 8).Value & "]").Selected = True
                '  .querySelector("#tZip-byaddress").Value = 10045
                .querySelector("#zip-by-address").Click
            End With

            While .Busy Or .readyState < 4: DoEvents: Wend

            t = Timer
            Do
                DoEvents
                On Error Resume Next
                Set ele = .document.querySelector("#zipByAddressDiv strong")
                On Error GoTo 0
                If Timer - t > MAX_WAIT_SEC Then Exit Do
            Loop While ele Is Nothing

            ws.Cells(i, 11) = ele.innerText
            Set ele = Nothing
        Next
        .Quit
    End With
End Sub
于 2018-10-04T17:03:20.297 回答