返回列表 上一主題 發帖

[發問] 個股歷史價格表

本帖最後由 GBKEE 於 2020-4-14 18:27 編輯

回復 5# Scott090
請了解 網頁控制項內容
--https://invest.cnyes.com/twstock/tws/2330/history
  1. '****選擇時間區間的各按鈕   [ 網頁控制項內容]  ********
  2. '<button class="jsx-197276814 period_btn" data-period="1d">1日</button>
  3. '<button class="jsx-197276814 period_btn" data-period="5d">5日</button>
  4. '<button class="jsx-197276814 period_btn" data-period="3m">3月</button>
  5. '<button class="jsx-197276814 period_btn" data-period="6m">6月</button>
  6. '<button class="jsx-197276814 period_btn" data-period="thisYear">今年以來</button>
  7. '<button class="jsx-197276814 period_btn" data-period="1y">1年</button>
  8. '<button class="jsx-197276814 period_btn" data-period="5y">5年</button>
  9. '<button class="jsx-197276814 period_btn" data-period="10y">最大</button>
  10. '<button class="jsx-197276814 period_btn period_btn--active" data-period="3m">3月</button>
  11. '****選擇時間區間後按下 (套用) 的按鈕   [ 網頁控制項內容]  ********
  12. '<button class="jsx-197276814 action_submit">套用</button>
  13. ''****選擇時間區間後按下(套用)按鈕所顯示的日期      [ 網頁控制項內容]  ********
  14. '<span class=    "jsx-197276814 btn_txt">2020/04/09 - 2020/04/14</span></button>
  15. ''****網頁所顯示股票的名稱      [ 網頁控制項內容]  ********
  16. '<h2 class="jsx-969407034 jsx-1444699802 sub_title">台積電</h2>
複製代碼
讀取網頁資料的程式碼 複製在同一模組中
  1. Option Explicit
  2. Sub Ex_鉅亨網_歷史價格()
  3.     Dim e As Object, R As Double, C As Integer
  4.      檢查
  5.     Application.ScreenUpdating = False
  6.     With CreateObject("InternetExplorer.Application")
  7.       '  .Visible = True
  8.         .Navigate "https://invest.cnyes.com/twstock/tws/" & [B1] & "/history"
  9.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  10.         If InStr(.Document.BODY.INNERTEXT, "您想看的頁面不存在") Then
  11.             .Quit    ''**   您想看的頁面不存在
  12.             MsgBox [B1] & "  找不到 ??? ":             End
  13.         End If
  14.         For Each e In .Document.ALL.TAGS("BUTTON")               '**指定時間區間
  15.             If e.CLASSNAME = "jsx-197276814 period_btn" And e.INNERTEXT = [D1].Text Then e.Click:  Exit For
  16.         Next
  17.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  18.         For Each e In .Document.ALL.TAGS("BUTTON")               '**指定時間區間後按下(套用)鍵
  19.             If e.CLASSNAME = "jsx-197276814 action_submit" And e.INNERTEXT = "套用" Then e.Click: Exit For
  20.         Next
  21.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  22.         For Each e In .Document.ALL.TAGS("H2")                            '**顯示股票的名稱
  23.             If e.CLASSNAME = "jsx-969407034 jsx-1444699802 sub_title" Then Range("B2") = e.INNERTEXT:  Exit For
  24.         Next
  25.         For Each e In .Document.ALL.TAGS("SPAN")                       '**顯示所指定的時間區間
  26.             If e.CLASSNAME = "jsx-197276814 btn_txt" Then Range("D2") = e.INNERTEXT:  Exit For
  27.         Next
  28.         '*********************等候資料下載完成***********************************************
  29.         Do While .Busy Or .readyState <> 4 And .Document.ALL.TAGS("table")(0) Is Nothing: DoEvents: Loop
  30.         '********************資料內容導入工作表***********
  31.         With .Document.ALL.TAGS("table")(0)
  32.             For R = 0 To .Rows.Length - 1
  33.                 For C = 0 To .Rows(R).Cells.Length - 1
  34.                     Cells(R + 3, C + 1) = .Rows(R).Cells(C).INNERTEXT
  35.                 Next
  36.             Next
  37.         End With
  38.         .Quit     '*********關閉網頁
  39.     End With
  40.     [f1] = "***資料下載 完成***"
  41.    Exit Sub
  42. End Sub
  43. Private Sub 檢查()
  44.          ''**選擇時間區間-> Range("B2") 的處存格 導入[驗證資料]
  45.         With Range("D1").Validation
  46.             .Delete
  47.             .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
  48.             xlBetween, Formula1:="1日,5日,3月,6月,今年以來,1年,5年,最大"   '
  49.         End With
  50.         If [B1] = "" Or [D1] = "" Then    '** Range("B2")  需是股票代號
  51.             MsgBox "股票號碼??  Or 時間區間 >>"
  52.              End         '***結束程式
  53.          End If
  54.         UsedRange.Offset(1).Clear
  55.       [f1] = "***資料下載中....***"
  56.       Application.ScreenUpdating = True
  57.       Application.Wait (Time + #12:00:01 AM#)
  58. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 口說好話、心想好意、身行好事。
返回列表 上一主題