我正在尝试从 Excel 中获取姓名和邮政编码列表,将它们依次输入一个姓名和邮政编码到 www.yellowpages.com 的搜索字段中,然后以与原始相同的顺序将街道地址结果返回到 Excel姓名和邮政编码。没有返回错误消息,它只是停止而没有完成。我不确定它在哪里停止,但它确实打开了 Internet Explorer,输入搜索词并单击搜索,因为当 .visible = True 时我可以看到。我最好的猜测是在“”之间。
这是我的代码(改编自DontFretBrett和Dinesh Kumar Takyar):
Sub Address_Scrape()
Dim eRow As Long
Dim ele As Object
Dim wb As Workbook
Dim srch As Worksheet
Dim trgt As Worksheet
Set wb = ThisWorkbook
Set srch = wb.Sheets("Master with addresses")
Set trgt = wb.Sheets("Sheet1")
Dim url As String
Dim zc As String
Dim Name As String
Name = srch.Range("B2")
zc = srch.Range("F2")
url = "URL;http://www.yellowpages.com/"
url = url & "/" & zc & "/" & Name
RowCount = 1
trgt.Range("A" & RowCount) = "Name"
trgt.Range("B" & RowCount) = "Address"
trgt.Range("C" & RowCount) = "City"
trgt.Range("D" & RowCount) = "State"
trgt.Range("E" & RowCount) = "Zip"
eRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Set objIE = CreateObject("InternetExplorer.Application")
With objIE
.navigate "http://www.yellowpages.com/"
.Visible = True
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
Set who = .document.getElementsByName("search_terms")
who.Item(0).Value = Name
Set where = .document.getElementsByName("geo_location_terms")
where.Item(0).Value = zc
.document.forms(0).submit
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
"Results = .document.getElementsByTagName("p")(0).innerText"
For Each ele In .document.all
Select Case ele.tagName
Case Results
RowCount = RowCount + 1
Case "Name"
trgt.Range("A" & RowCount) = ele.getElementByclass("business-name").innerText
Case "Address"
trgt.Range("B" & RowCount) = ele.getElementByclass("street-address").innerText
Case "City"
trgt.Range("C" & RowCount) = Trim(ele.getElementByclass("locality").innerText)
Case "State"
trgt.Range("D" & RowCount) = ele.getElementByitemprop("addressRegion").innerText
Case "Zip"
trgt.Range("E" & RowCount) = ele.getElementByitemprop("postalCode").innerText
End Select
Next ele
Set objIE = Nothing
End With
End Sub