Html Excel VBA激活web中的selectitem并上网获取数据
我对VBA非常熟悉。我尝试使用互联网上所有可能的解决方案在web上激活selectitem并获取数据表,但没有成功。下面是我的Excel VBA代码和JavascriptHtml Excel VBA激活web中的selectitem并上网获取数据,html,excel,vba,web-scraping,onchange,Html,Excel,Vba,Web Scraping,Onchange,我对VBA非常熟悉。我尝试使用互联网上所有可能的解决方案在web上激活selectitem并获取数据表,但没有成功。下面是我的Excel VBA代码和Javascript Sub GetQuarterFinancials() Dim ie As Object, URL As String URL = "https://www.marketwatch.com/investing/stock/aapl/financials" Set ie = CreateObject("inte
Sub GetQuarterFinancials()
Dim ie As Object, URL As String
URL = "https://www.marketwatch.com/investing/stock/aapl/financials"
Set ie = CreateObject("internetexplorer.application")
With ie
.Visible = True
.navigate URL
Do While .Busy
DoEvents
Loop
Do While .readyState <> 4
DoEvents
Loop
Set ticker = ie.document.getElementById("autocomplete_input")
ticker.value = "FB"
.document.getElementById("investing_ac_button").Click
Application.Wait (Now + TimeValue("00:00:05"))
Set selectitem = ie.document.getElementsByTagName("select")
For Each i In selectitem
i.value = "/investing/stock/msci/financials/income/quarter"
i.FireEvent ("onchange")
Next i
End With
End Sub
<select style="float:right" onchange="window.location = this.options[selectedIndex].value;">
<option value="/investing/stock/msci/financials" selected="selected">Annual Financials</option>
<option value="/investing/stock/msci/financials/income/quarter" >Quarter Financials</option>
</select>
<div style="clear:both"></div>
</div>
<div class="block">
<h2>Annual Financials for MSCI Inc.</h2>
您可以使用从下拉列表中选择适当的选项,然后选择它
Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub MakeSelections()
Dim ie As New InternetExplorer
With ie
.Visible = True
.Navigate2 "https://www.marketwatch.com/investing/stock/aapl/financials"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
.querySelector("#autocomplete_input").Value = "FB" 'you could avoid search by adding ticker into url
.querySelector("#investing_ac_button").Click
End With
While .Busy Or .readyState < 4: DoEvents: Wend
Dim ele As Object
With .document
Do
On Error GoTo 0
Set ele = .querySelector("[value^='/investing/stock/fb/financials/income/quarter']")
On Error Resume Next
Loop While ele Is Nothing
.querySelector("[value^='/investing/stock/fb/financials/income/quarter']").Selected = True
.querySelector(".financials select").FireEvent "onchange"
Stop 'delete me later
End With
.Quit
End With
End Sub
将股票代码添加到url中肯定是最好的方法。在web中选择“季度财务”,但在页面中不刷新。如何将其编码为季度数据。谢谢您可能需要在选择元素第二个问题上更改fireevent。如何在股票代码后面对“季度财务”进行编码?股票代码是变体。ThanksI添加了.querySelector[value^='/investment/stock/fb/financials/income/quarter']。fireevent一旦更改,但不起作用。我的代码正确吗?