【发布时间】:2019-10-21 22:42:25
【问题描述】:
我想将餐厅名称、电话号码、网站和地址等餐厅数据导入 Excel,但不幸的是我收到了广告和垃圾数据。我已经使用http://automatetheweb.net/vba-getelementsbytagname-method/ 网站创建了一个代码,但它没有帮助。请纠正我的代码中的问题。
网址:https://www.yellowpages.com/atlanta-ga/attorneys
请不要引用 json,因为它不适用于其他网站。
Sub Yellowcom()
'Dim ieObj As InternetExplorer
Dim htmlELe As IHTMLElement
Dim HTML As HTMLDocument
Dim i As Integer
Dim URL As String
Dim URLParameter As String
Dim page As Long
Dim links As Object
Dim IE As Object
i = 1
Set IE = CreateObject("InternetExplorer.Application")
'Set ieObj = New InternetExplorer
IE.Visible = True
URL = "https://www.yellowpages.com/atlanta-ga/attorneys"
'Application.Wait Now + TimeValue("00:00:05")
For page = 2 To 4
If page > 1 Then URLParameter = "?page=" & page
IE.navigate URL & URLParameter
' Wait for the browser to load the page
Do Until IE.readyState = 4
DoEvents
Loop
Set HTML = IE.document
Set links = HTML.getElementsByClassName("info")
For Each htmlELe In links
With ActiveSheet
.Range("A" & i).Value = htmlELe.Children(0).textContent
.Range("B" & i).Value = htmlELe.getElementsByTagName("a")(0).href
.Range("C" & i).Value = htmlELe.Children(2).textContent
.Range("D" & i).Value = htmlELe.Children(2).querySelector("a[href]")
'links2 = htmlELe.getElementsByClassName("links")(1)
' .Range("D" & i).Value = links2.href
End With
i = i + 1
Next htmlELe
Next page
IE.Quit
Set IE = Nothing
End Sub
【问题讨论】:
-
你遇到了什么错误?
-
我在网站部门中得到了添加,地址字段给出了页面地址
标签: excel vba web-scraping getelementsbyclassname