如何使用Chrome浏览器使用VBA进行网络抓取Twitter?



我正在尝试用VBA刮Twitter。该代码适用于其他带有Internet Explorer的网站,但是由于Internet Explorer无法打开Twitter网站,因此我正在尝试将其替换为Chrome浏览器。我找到了如何在Chrome中打开URL,但不知道应该输入什么才能从HTML文档中检索数据。下面,我保留了与Internet Explorer一起使用的代码,并添加了打开Chrome浏览器的代码。我的主要问题是我应该放什么而不是"?????????",在以下代码中:

Sub GetData()

Dim objIE As InternetExplorer
Dim itemEle As Object
Dim desc As String, a As String, title As String, titleDate As String

Dim y As Integer
Dim sURL As String
Dim lastrow As Long

Dim chromePath As String

chromePath = """C:Program Files (x86)GoogleChromeApplicationchrome.exe"""

lastrow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row
y = 2

For i = 2 To lastrow


sURL = Sheet1.Cells(i, 1)
'Set objIE = New InternetExplorerMedium
'objIE.Visible = True

Shell (chromePath & Sheets("profileLinks").Cells(i, 1))

'objIE.navigate sURL
'Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

On Error GoTo err_clear
Application.Wait (Now + TimeValue("0:00:10"))


For Each itemEle In ?????????.document.getElementsByClassName("css-901oao css-bfa6kz r-111h2gw r-18u37iz r-1qd0xha r-a023e6 r-16dba41 r-ad9z0x r-bcqeeo r-qvutc0")
Text = itemEle.getElementsByTagName("span")(0).innerText

Sheets("Outcome").Range("A" & y).Value = Text
y = y + 1
Next

'objIE.Quit
Next i

err_clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If

End Sub

请随时提出任何其他解决方案。提前谢谢你。

我认为您需要额外的对象变量来保存对 html 对象库中对象的引用。例如,在引用Microsoft HTML 对象库后,您可以像这样添加 sth:

Dim HTMLDocument As MSHTML.HTMLDocument
Dim itemEle As MSHTML.IHTMLElement
Dim itemCollection AS MSHTML.IHTMLElementCollection
Set HTMLDocument = objIE.Document
Set itemCollection = HTMLDocument.getElementsByClassName("...")
For Each itemEle in itemCollection
'extra code here
Next itemEle

我希望这能回答你的问题!

最新更新