【发布时间】:2019-05-15 08:46:26
【问题描述】:
我有一些经验和知识如何通过 tagName 或 ClassName 抓取。但是,在这种特殊情况下,className 不是唯一的,并且链接在访问页面后一直在变化,因此无法获得直接链接。唯一独特的组合是类和链接文本。例如Budget and Forecast updating 和a_1_610 和Budget and Forecast updating 和a_1_611 访问的代码是什么?
我的代码(根据 QHarr 回答编辑):
Sub GoToLiinosBot()
'This will load a webpage in IE
Dim ie As InternetExplorer
Dim HWNDSrc As Long
Dim elements As Object
Set ie = Nothing
Set ie = New InternetExplorerMedium
ie.Visible = True
ie.Navigate "http://link.com"
With ie
Do
DoEvents
Loop Until ie.ReadyState = READYSTATE_COMPLETE
End With
Application.Wait (Now + TimeValue("0:00:04"))
ie.Document.querySelector(".data .a_1_611").innerText
'Unload IE
Set ie = Nothing
End Sub
这里是源代码:
【问题讨论】:
-
有多种方法可以实现这一点,您可以获取包含所需类的节点集合,然后遍历那些评估 inner.text 或 inner.html 以查看对于您想要的节点(使用 .getElementsByClassName() see this answer) - 另一种可以说是最有效的方法是使用
.querySelector()直接定位节点,如 this question / w3schools link -
Another resource for the selectors(未编辑,因为达到字符限制)特别是:
[attribute*=value] a[href*="w3schools"] Selects every <a> element whose href attribute value contains the substring "w3schools"
标签: html excel vba web-scraping