Html VBA web抓取:单击列表中的元素
我想写一个vba程序,从网页上自动下载历史股票数据。 我要选择的元素的对应缩进HTML代码如下图所示: 我的VBA代码如下所示:Html VBA web抓取:单击列表中的元素,html,vba,web-scraping,Html,Vba,Web Scraping,我想写一个vba程序,从网页上自动下载历史股票数据。 我要选择的元素的对应缩进HTML代码如下图所示: 我的VBA代码如下所示: Dim IE As SHDocVw.InternetExplorer Dim HTMLDoc As mshtml.HTMLDocument Dim HTMLCurButton As mshtml.IHTMLElement Set IE = New SHDocVw.InternetExplorer IE.Visible = True I
Dim IE As SHDocVw.InternetExplorer
Dim HTMLDoc As mshtml.HTMLDocument
Dim HTMLCurButton As mshtml.IHTMLElement
Set IE = New SHDocVw.InternetExplorer
IE.Visible = True
IE.FullScreen = False
IE.Resizable = True
IE.Navigate "https://www.dukascopy.com/swiss/english/marketwatch/historical/"
Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop
Set HTMLDoc = IE.Document
Set HTMLCurButton = HTMLDoc.getElementById(":6n")
HTMLCurButton.Click
Dim IE作为SHDocVw.InternetExplorer
将HTMLDoc设置为mshtml.HTMLDocument
将HTMLCURBUTON变暗为mshtml.IHTMLElement
设置IE=New SHDocVw.InternetExplorer
可见=真实
IE.FullScreen=False
即:可调整大小=真
即“导航”https://www.dukascopy.com/swiss/english/marketwatch/historical/"
在IE.ReadyState ReadyState\u完成时执行此操作
环
设置HTMLDoc=IE.Document
设置HTMLCurButton=HTMLDoc.getElementById(“:6n”)
HTMLCURBUTON。单击
但不幸的是,对象HTMLCurButton仍然是空的
我非常感谢你的帮助 一个非常有趣的问题。我认为你不能用Internet Explorer解决这个问题。这是因为列表位于受保护的iframe中,即您无法导航到包含列表的内部文档的src 使用带Chrome的selenium basic vba,可以切换到适当的iframe。然后需要满足几个测试条件,以便有足够的时间使页面上的操作生效
Option Explicit
Public Sub MakeChanges()
'VBE > Tools > References > Selenium Type Library
'Download: https://github.com/florentbr/SeleniumBasic/releases/tag/v2.0.9.0
Dim d As WebDriver, t As Date
Set d = New ChromeDriver
Const url = "https://www.dukascopy.com/swiss/english/marketwatch/historical/"
With d
.Start "Chrome"
.get url
.SwitchToFrame .FindElementByCss("script + iframe")
t = Timer
Do
With .FindElementByCss("[data-instrument='A.US/USD']")
.Click
If .Attribute("aria-selected") Then Exit Do
End With
Loop While Timer - t < 10
With .FindElementByCss(".d-wh-vg-v-p > div")
If Not .Attribute("aria-disabled") Then .Click
End With
'other code now presented with accept and then login details
Stop
.Quit
End With
End Sub
选项显式
公共子系统进行更改()
'VBE>Tools>References>Selenium类型库
'下载:https://github.com/florentbr/SeleniumBasic/releases/tag/v2.0.9.0
Dim d作为Web驱动程序,t作为日期
设置d=新的色度驱动器
常量url=”https://www.dukascopy.com/swiss/english/marketwatch/historical/"
与d
.启动“Chrome”
.获取url
.SwitchToFrame.FindElementByCss(“脚本+iframe”)
t=计时器
做
带.FindElementByCss(“[data instrument='A.US/USD']”)
点击
如果.Attribute(“aria selected”),则退出Do
以
定时循环-t<10
使用.FindElementByCss(“.d-wh-vg-v-p>div”)
如果没有.Attribute(“aria已禁用”),则单击
以
'其他代码现在显示接受和登录详细信息
停止
退出
以
端接头
我认为这是因为您需要的对象位于iframe中。非常感谢!这正是我想要的。现在我尝试用以下代码更改日期:'with.FindElementByCss(“a-b-c.a-ab-v-y-x>div”).FindElementByTag(“span”).innerText=“2017-01-01”End with”但不起作用。作为一个新问题解释错误,我会在computerOk上看一看。我刚刚发布了一个新问题:“VBA网页抓取-更改日历日期”谢谢!