返回列表 上一主題 發帖

股價代號和起始日期的抓取

回復 1# gelai01000001
下載3年歷史股價的資料
  1. Option Explicit
  2. Sub 鉅亨網_歷史行情()
  3.     Dim Sh As Worksheet, Code As String, d_Start As String, d_End  As String
  4.     Dim A As Object, i As Integer, c As Integer, T As Date
  5.     Code = InputBox("輸入股票代號 :  ", "股票代號", 2303)
  6.     d_End = InputBox("輸入結束日期 :  ", "結束日期", Date)
  7.     If Len(Code) <= 3 Or Not IsDate(d_End) Then Exit Sub
  8.     Set Sh = ActiveSheet
  9.     With Sh
  10.         .UsedRange.Clear
  11.         .[a1] = Code
  12.         .[b1] = DateAdd("yyyy", -3, d_End)  '下載3年歷史股價的資料
  13.         .[C1] = d_End
  14.         Code = .[a1]
  15.         d_Start = Format(.[b1], "yyyy/mm/dd")
  16.         d_End = Format(.[C1], "yyyy/mm/dd")
  17.     End With
  18.     With CreateObject("InternetExplorer.application")
  19.         .Navigate "http://www.cnyes.com/twstock/ps_historyprice/" & Code & ".htm"
  20.         '.Visible = True
  21.         Application.StatusBar = Code & " 歷史行情 等候中..."
  22.         Do While .Busy Or .ReadyState <> 4
  23.              DoEvents
  24.         Loop
  25.         Set A = .Document.getelementsbytagname("input")
  26.         A(5).Value = d_Start
  27.         A(6).Value = d_End
  28.         A(7).Click
  29.         T = Time
  30.         Do
  31.             DoEvents
  32.         Loop Until Time > T + #12:00:08 AM#
  33.         Set A = .Document.getelementsbytagname("table")(1)
  34.         Application.StatusBar = Code & " 歷史行情 下載中..."
  35.         Cells(2, 1) = .Document.getelementsbytagname("span")(79).innertext
  36.         For i = 0 To A.Rows.Length - 1
  37.             For c = 0 To A.Rows(i).Cells.Length - 1
  38.                 Sh.Cells(i + 3, c + 1) = A.Rows(i).Cells(c).innertext
  39.             Next
  40.         Next
  41.        .Quit
  42.     End With
  43.     Application.StatusBar = Code & " 歷史行情 " & Application.Text(Time - T, "[S] 秒") & " 下載完成"
  44.     MsgBox "OK"
  45.     Application.StatusBar = False
  46. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 3# gelai01000001
CreateObject("InternetExplorer.application") 多搜尋一下會多了解的
  1. With CreateObject("InternetExplorer.application")
  2.     '歷史行情 預設為一個月資料,日期的更改需在網頁上更改
  3.         .Navigate "http://www.cnyes.com/twstock/ps_historyprice/" & Code & ".htm"  'IE的網址
  4.         '.Visible = True
  5.         Application.StatusBar = Code & " 歷史行情 等候中..."
  6.         Do While .Busy Or .ReadyState <> 4     '等候網頁資料下載完成...
  7.              DoEvents
  8.         Loop
  9.         Set A = .Document.getelementsbytagname("input")  '網頁中成員 tagname為 "input"
  10.         A(5).Value = d_Start    '0 開始算起第6個 => 5     開始日期
  11.         A(6).Value = d_End      '0 開始算起第7個 => 6     結束日期
  12.         A(7).Click              '0 開始算起第8個 => 7     按下查詢鍵
  13.         T = Time
  14.         Do
  15.             DoEvents
  16.         Loop Until Time > T + #12:00:08 AM#              '按下查詢鍵後,需等候網頁資料下載完成
  17.         Set A = .Document.getelementsbytagname("table")(1) ''0 開始算起第2個 => 1
  18.         '網頁中成員 tagname為 "table" ,為資料所在的位置.
  19.         Application.StatusBar = Code & " 歷史行情 下載中..."
  20.         Cells(2, 1) = .Document.getelementsbytagname("span")(79).innertext  'innertext: 文字
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 5# edwardlay
2# 看要幾年可修改 -3  看看
  1. 12.        .[b1] = DateAdd("yyyy", -3, d_End)  '下載3年歷史股價的資料
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 7# gelai01000001
試試看
  1. Option Explicit
  2. Sub 下載網站資料()
  3.     Dim Startmonth As String, Startday As String, xDate As Date, Msg As Boolean, Sh As Worksheet
  4.     Dim Wb As Workbook, Ws As Worksheet, Stock  As Range
  5.     On Error GoTo EX                        '程式執行中有錯誤時跳到 EX: 繼續執行程式
  6.     xDate = Date                            '當日
  7.     Set Sh = Workbooks.Add.Sheets(1)        '新活頁簿的第一個工作表
  8. EX:
  9.     If Err.Number <> 0 Or Msg = True Then   '當日(尚未有資料:錯誤) 或 休市
  10.         xDate = xDate - 1                   '往後退一天一直到開市. ( 假如休市超過1天以上(2,3,4,5,6,春節9天)
  11.         Err.Clear
  12.         Msg = False
  13.     End If
  14.     Startday = Format(xDate, "YYYYMMDD")
  15.     Startmonth = Format(xDate, "YYYYMM")
  16.         '下載網站的網址
  17.     With Sh.QueryTables.Add(Connection:="URL;http://www.twse.com.tw/ch/trading/exchange/MI_INDEX/genpage/Report" & Startmonth & "/A112" & Startday & "ALLBUT0999_1.php?select2=ALLBUT0999&chk_date=" & Format(xDate, "E/MM/DD"), Destination:=Range("A3"))
  18.         .RefreshStyle = xlOverwriteCells                                '抓取網頁的第10個表格作為匯入資料
  19.         .WebTables = "10"
  20.         .Refresh BackgroundQuery:=False                                 '當日(尚未有資料:錯誤)
  21.         If Application.CountA(Sh.QueryTables(1).ResultRange) = 0 Then   '休市 沒有資料
  22.             Msg = True
  23.             GoTo EX
  24.         End If
  25.     End With
  26.     For Each Wb In Workbooks
  27.         For Each Ws In Wb.Sheets
  28.            Set Stock = Sh.[A:A].Find(Ws.Name, lookat:=xlWhole)
  29.             If Not Stock Is Nothing Then
  30.                 Stock.Offset(, 2).Resize(, 14).Copy
  31.                 With Ws
  32.                     With .Range("a" & .Rows.Count).End(xlUp).Offset(1)
  33.                         .Cells = xDate
  34.                         .Offset(, 1).PasteSpecial xlPasteValuesAndNumberFormats
  35.                     End With
  36.                 End With
  37.             End If
  38.         Next
  39.     Next
  40.     Sh.Parent.Close False           '關閉: 新活頁簿的第一個工作表
  41. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2016-4-26 10:25 編輯

回復 9# BigDog
2016/4/26 更新
  1. Option Explicit
  2. Sub 鉅亨網_董監持股()
  3.     Dim ie As Object, A As Object, T As Date, i As Integer, c As Integer
  4.     Dim Code As String, xMonth As String
  5.     Code = InputBox("輸入股票代號 :  ", "股票代號", 2303)
  6.     xMonth = InputBox("輸入年月 :  ", "年月", Format(DateAdd("M", -2, Date), "yyyy年m月"))
  7.     Set ie = CreateObject("InternetExplorer.Application")
  8.     ie.Navigate "http://www.cnyes.com/twstock/directorholder/" & Code & ".htm"
  9.    '  ie.Visible = True
  10.     Do While ie.Busy Or ie.ReadyState <> 4: DoEvents: Loop
  11.     With ie.Document
  12.          With .getElementById("ctl00_ContentPlaceHolder1_DropDownList1")
  13.                 .Value = xMonth
  14.                 .fireEvent ("onchange")
  15.         End With
  16.         Do While ie.Busy Or ie.ReadyState <> 4
  17.              DoEvents
  18.         Loop
  19.         On Error Resume Next
  20.         Do
  21.                 DoEvents
  22.                 Set A = .getelementsbytagname("table")(1)
  23.                 If Not A Is Nothing Then
  24.                         If InStr(A.innertext, Format(xMonth, "YYYYM")) Then
  25.                         If Err Then
  26.                            Debug.Print Err
  27.                             Err.Clear
  28.                         Else
  29.                             Exit Do
  30.                         End If
  31.                         End If
  32.                 End If
  33.         Loop
  34.         On Error GoTo 0
  35.         Cells.Clear
  36.         Cells(1, "A").Resize(, 4) = Array("股票代號", Code, "月份", xMonth)
  37.         For i = 0 To A.Rows.Length - 1
  38.             For c = 0 To A.Rows(i).Cells.Length - 1
  39.                 Cells(i + 2, c + 1) = A.Rows(i).Cells(c).innertext
  40.             Next
  41.         Next
  42.     End With
  43.       ie.Quit
  44.     MsgBox "OK"
  45. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 14# omit
你可試試看,如不行可上傳檔案看看
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 19# yopoyuan
將你的發問合併在[指定下載3年歷史股價的資料] ,請從第1帖看起.
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 能付出愛心就是福,能消除煩惱就是慧。
返回列表 上一主題