我想导入餐厅数据,例如餐厅名称,电话号码,网站和地址,以使其表现出色,但不幸的是,我正在获取广告和垃圾数据。我已经使用http://automatetheweb.net/vba-getelementsbytagname-method/网站创建了一个代码,但是没有帮助。请纠正我的代码中的问题。
网站:https://www.yellowpages.com/atlanta-ga/attorneys
请不要引用json,因为它不能在其他网站上使用。
myView.convert(gestureRecognizer.location(in: myView), to: nil)
答案 0 :(得分:2)
我将使用xhr而不是浏览器,并将数据存储在每个页面的数组中,并将其写到工作表中。您实际上可以根据每页的结果和页数预先确定一个数组的大小,以预先保存所有结果,但是下面的方法仍然有效
Option Explicit
Public Sub GetListings()
Dim html As HTMLDocument, page As Long, html2 As HTMLDocument
Dim results As Object, headers(), ws As Worksheet, i As Long
Const START_PAGE As Long = 1
Const END_PAGE As Long = 2
Set ws = ThisWorkbook.Worksheets("Sheet1")
headers = Array("Name", "Phone", "Website", "Address")
Application.ScreenUpdating = False
Set html = New HTMLDocument
Set html2 = New HTMLDocument
ws.Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
With CreateObject("MSXML2.XMLHTTP")
For page = START_PAGE To END_PAGE
.Open "GET", "https://www.yellowpages.com/atlanta-ga/attorneys?page=" & page, False
.send
html.body.innerHTML = .responseText
Set results = html.querySelectorAll(".organic .result")
Dim output(), r As Long
ReDim output(1 To results.Length, 1 To 4)
r = 1
For i = 0 To results.Length - 1
On Error Resume Next
html2.body.innerHTML = results.item(i).outerHTML
output(r, 1) = html2.querySelector(".business-name").innerText
output(r, 2) = html2.querySelector(".phone").innerText
output(r, 3) = html2.querySelector(".track-visit-website").href
output(r, 4) = html2.querySelector(".street-address").innerText & " " & html2.querySelector(".locality").innerText
On Error GoTo 0
r = r + 1
Next
ws.Cells(GetLastRow(ws, 1) + 1, 1).Resize(UBound(output, 1), UBound(output, 2)) = output
Next
End With
Application.ScreenUpdating = True
End Sub
Public Function GetLastRow(ByVal ws As Worksheet, Optional ByVal columnNumber As Long = 1) As Long
With ws
GetLastRow = .Cells(.rows.Count, columnNumber).End(xlUp).Row
End With
End Function
输出示例:
答案 1 :(得分:1)
信息类也用于广告。您首先需要转到类名称为“自然搜索结果”的集合,然后在其中找到所有“信息”类。
这意味着您需要一个额外的集合变量:
Set HTML = IE.document
Set OrganicLinks = HTML.getElementsByClassName("search-results organic")
Set links = OrganicLinks.item(0).getElementsByClassName("info")
要获得正确的网站,您需要使用其他参考。最好通过类名来获取它,因为它更独特:
On Error Resume Next
.Range("B" & i).Value = htmlELe.getElementsByClassName("track-visit-website")(0).href
On Error GoTo 0