- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
本帖最後由 GBKEE 於 2020-4-14 18:27 編輯
回復 5# Scott090
請了解 網頁控制項內容
--https://invest.cnyes.com/twstock/tws/2330/history- '****選擇時間區間的各按鈕 [ 網頁控制項內容] ********
- '<button class="jsx-197276814 period_btn" data-period="1d">1日</button>
- '<button class="jsx-197276814 period_btn" data-period="5d">5日</button>
- '<button class="jsx-197276814 period_btn" data-period="3m">3月</button>
- '<button class="jsx-197276814 period_btn" data-period="6m">6月</button>
- '<button class="jsx-197276814 period_btn" data-period="thisYear">今年以來</button>
- '<button class="jsx-197276814 period_btn" data-period="1y">1年</button>
- '<button class="jsx-197276814 period_btn" data-period="5y">5年</button>
- '<button class="jsx-197276814 period_btn" data-period="10y">最大</button>
- '<button class="jsx-197276814 period_btn period_btn--active" data-period="3m">3月</button>
- '****選擇時間區間後按下 (套用) 的按鈕 [ 網頁控制項內容] ********
- '<button class="jsx-197276814 action_submit">套用</button>
- ''****選擇時間區間後按下(套用)按鈕所顯示的日期 [ 網頁控制項內容] ********
- '<span class= "jsx-197276814 btn_txt">2020/04/09 - 2020/04/14</span></button>
- ''****網頁所顯示股票的名稱 [ 網頁控制項內容] ********
- '<h2 class="jsx-969407034 jsx-1444699802 sub_title">台積電</h2>
複製代碼 讀取網頁資料的程式碼 複製在同一模組中- Option Explicit
- Sub Ex_鉅亨網_歷史價格()
- Dim e As Object, R As Double, C As Integer
- 檢查
- Application.ScreenUpdating = False
- With CreateObject("InternetExplorer.Application")
- ' .Visible = True
- .Navigate "https://invest.cnyes.com/twstock/tws/" & [B1] & "/history"
- Do While .Busy Or .readyState <> 4: DoEvents: Loop
- If InStr(.Document.BODY.INNERTEXT, "您想看的頁面不存在") Then
- .Quit ''** 您想看的頁面不存在
- MsgBox [B1] & " 找不到 ??? ": End
- End If
- For Each e In .Document.ALL.TAGS("BUTTON") '**指定時間區間
- If e.CLASSNAME = "jsx-197276814 period_btn" And e.INNERTEXT = [D1].Text Then e.Click: Exit For
- Next
- Do While .Busy Or .readyState <> 4: DoEvents: Loop
- For Each e In .Document.ALL.TAGS("BUTTON") '**指定時間區間後按下(套用)鍵
- If e.CLASSNAME = "jsx-197276814 action_submit" And e.INNERTEXT = "套用" Then e.Click: Exit For
- Next
- Do While .Busy Or .readyState <> 4: DoEvents: Loop
- For Each e In .Document.ALL.TAGS("H2") '**顯示股票的名稱
- If e.CLASSNAME = "jsx-969407034 jsx-1444699802 sub_title" Then Range("B2") = e.INNERTEXT: Exit For
- Next
- For Each e In .Document.ALL.TAGS("SPAN") '**顯示所指定的時間區間
- If e.CLASSNAME = "jsx-197276814 btn_txt" Then Range("D2") = e.INNERTEXT: Exit For
- Next
- '*********************等候資料下載完成***********************************************
- Do While .Busy Or .readyState <> 4 And .Document.ALL.TAGS("table")(0) Is Nothing: DoEvents: Loop
- '********************資料內容導入工作表***********
- With .Document.ALL.TAGS("table")(0)
- For R = 0 To .Rows.Length - 1
- For C = 0 To .Rows(R).Cells.Length - 1
- Cells(R + 3, C + 1) = .Rows(R).Cells(C).INNERTEXT
- Next
- Next
- End With
- .Quit '*********關閉網頁
- End With
- [f1] = "***資料下載 完成***"
- Exit Sub
- End Sub
- Private Sub 檢查()
- ''**選擇時間區間-> Range("B2") 的處存格 導入[驗證資料]
- With Range("D1").Validation
- .Delete
- .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
- xlBetween, Formula1:="1日,5日,3月,6月,今年以來,1年,5年,最大" '
- End With
- If [B1] = "" Or [D1] = "" Then '** Range("B2") 需是股票代號
- MsgBox "股票號碼?? Or 時間區間 >>"
- End '***結束程式
- End If
- UsedRange.Offset(1).Clear
- [f1] = "***資料下載中....***"
- Application.ScreenUpdating = True
- Application.Wait (Time + #12:00:01 AM#)
- End Sub
複製代碼 |
|