【发布时间】:2019-05-25 01:51:20
【问题描述】:
我在 Excel 中使用 VBA,并尝试访问网页,然后单击链接以在我的 PC 上触发 CSV 下载。
到目前为止,我可以使用 VBA 打开 IE 并访问所需的网页。但是在检查了页面的 HTML 代码 (F12) 并使用 getElementsByClassName 和其他方法之后,我无法生成一个可点击的对象来触发我的代码下载。
Sub Automate_IE_Enter_Data() '这将在 IE 中加载一个网页 暗淡我只要 将 URL 变暗为字符串 将 IE 调暗为对象 将 objElement 作为对象调暗 将 objCollection 调暗为对象 将 HWNDSrc 变暗
'Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
'URL = "https://www.automateexcel.com/excel/vba"
URL = "https://www.barchart.com/futures/quotes/ZWF9|530P/price-history/historical"
'Navigate to URL
IE.Navigate URL
' Statusbar let's user know website is loading
Application.StatusBar = URL & " is loading. Please wait..."
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertantly skipping over the second loop)
'Do While IE.ReadyState = 4: DoEvents: Loop
Do Until IE.ReadyState = 4: DoEvents: Loop
'Webpage Loaded
Application.StatusBar = URL & " Loaded"
'Get Window ID for IE so we can set it as activate window
HWNDSrc = IE.hWnd
'Set IE as Active Window
SetForegroundWindow HWNDSrc
Set IEAppColl = IE.Document.getElementsByClassName("bc-glyph-download")(0)
IEAppColl.Click
Application.Wait Now + TimeValue("00:00:10")
SetForegroundWindow HWNDSrc
Application.Wait Now + TimeValue("00:00:05")
结束子
预期:1) 在 IE 上打开以下 URL: https://www.barchart.com/futures/quotes/ZWF9|530P/price-history/historical 2) 点击每日价格部分的“最高”链接下载 CSV 文件
1) 没问题 2) 产生错误:对象不支持此属性或方法。
关于如何在我的 VBA 代码中复制“最大”链接的点击,我没有点击。不确定我使用的是正确的名称。
【问题讨论】:
-
请使用sn-p工具通过edit分享相关html
标签: excel vba web-scraping