返回列表 上一主題 發帖

[發問] 想請教一個抓歷史股價的程式

[發問] 想請教一個抓歷史股價的程式

如附件 回測表
我打算利用 symbol 和 日期 去如鉅亨網/yahoo/google抓幾個日期的歷史股價下來
本來有做一個所有symbol的歷史股價。但只要抓幾個日期,卻要抓2012/6/1~2014/5/31所有資料,感覺應該有更好的方式。

請問有什麼好的程式寫法或想法嗎?謝謝

insiderMonkey(0608-1簡化).rar (728.59 KB)

小菜鳥一個 請多指教~

回復 11# kasl
試試看
  1. Option Explicit
  2. Const Code_txt = "D:\Code.Txt"
  3. Const FormDLL = "FM20.DLL"
  4. Sub Ex_Ie_下一頁()
  5.     Dim IE As Object, URL As String, E As Variant, i As Integer
  6.     Dim StartDate As Date, EndDate As Date
  7.     Dim A As Variant, Table As Object, Ar_Code(), Code As Variant
  8.     Set_FormDLL
  9.     StartDate = DateAdd("yyyy", -1, Date) '1年前的日期
  10.     'StartDate = DateAdd("m", -1, Date)    '1個月前的日期
  11.     EndDate = Date
  12.     MsgBox EndDate & " -- " & StartDate
  13.     Ar_Code = Array("sgen", "AMEH", "HMNC")  'Code 的陣列
  14.     'Ar_Ccod() = Array("sgen", "AMEH", "HMNC", "OZM", "ARCC", "TDG", "ECL", "AN")
  15.     Set IE = CreateObject("InternetExplorer.Application")
  16.     With IE
  17.         For Each Code In Ar_Code
  18.             If Dir(Code_txt) <> "" Then Kill Code_txt
  19.             URL = "http://www.cnyes.com/USAstock/history.aspx?code=" & Code
  20.          '   .Visible = True     '  是否顯示 IE
  21.             .Navigate URL
  22.             Application.StatusBar = Code & " 網頁 開啟中..."
  23.             Do While .Busy Or .readyState <> 4:  DoEvents:       Loop
  24.             If .LocationURL = "http://www.cnyes.com/usastock/index.htm" Then
  25.                 MsgBox "Code 找不到 " & Code
  26.                 GoTo Code_Next
  27.             End If
  28.             Application.StatusBar = Code & "日期 " & EndDate & " -- " & StartDate & " 指定中..."

  29.             With .document.getElementsByTagName("SELECT")           '月份輸入
  30.                 .Item("startMonth").Value = Month(StartDate) - 1    '開始月份
  31.                 .Item("endMonth").Value = Month(EndDate) - 1        '結束月份
  32.             End With
  33.             With .document.getElementsByTagName("INPUT")
  34.                 .Item("startDay").Value = Day(StartDate)            '開始日期
  35.                 .Item("startDay").Value = Day(StartDate)            '開始日期
  36.                 .Item("startYear").Value = Year(StartDate)          '開始年度
  37.                 .Item("endDay").Value = Day(EndDate)                '結束日期
  38.                 .Item("endYear").Value = Year(EndDate)              '結束年度
  39.                 .Item("perPage").Value = 100                        '顯示資料的筆數
  40.             End With
  41.             For Each E In .document.getElementsByTagName("BUTTON")
  42.                 If E.Type = "submit" Then
  43.                     E.Click                                         '按下搜尋鍵
  44.                     Exit For
  45.                 End If
  46.             Next
  47.             Application.StatusBar = "按下搜尋鍵 等候網頁中... "
  48.             Do While .Busy Or .readyState <> 4:   DoEvents:       Loop
  49.             Application.Wait Time + #12:00:10 AM#                   '等候網頁
  50.             Set Table = .document.getElementsByTagName("TABLE")
  51.             For Each E In .document.getElementsByTagName("SPAN")
  52.                 If InStr(E.innerText, "Page   of") Then
  53.                     i = Val(Replace(E.innerText, "Page   of", ""))   '取得資料總頁數
  54.                     Exit For
  55.                 End If
  56.             Next
  57.             On Error GoTo Ie_Err
  58.             For A = 0 To i
  59.                 Application.StatusBar = Code & "  " & EndDate & " -- " & StartDate & "共 " & i & " 頁 下載  第 " & A + IIf(A = 0, 1, 0) & " 中..."
  60.                 For Each E In .document.getElementsByTagName("A")
  61.                     If Trim(E.innerText) = ">" Then
  62.                         If A > 1 Then E.Click                          '下一頁按鍵
  63.                             Do While .Busy Or .readyState <> 4:   DoEvents:       Loop
  64.                             Application.Wait Time + #12:00:05 AM#                '等候網頁
  65.                             Set Table = .document.getElementsByTagName("TABLE")
  66.                             Exit For
  67.                         End If
  68.                 Next
  69.                 If A = 0 Or A > 1 Then
  70.                 Close #1
  71.                 Open Code_txt For Append As #1
  72.                 Print #1, Table(12).outerHTML
  73.                 Close #1
  74.                 End If
  75.             Next
  76.             Date_of_refresh Code, A  '導入資料程式 要給參數 Code , A
  77. Code_Next:
  78.         Next
  79.         .Quit
  80.     End With
  81.     Application.StatusBar = False
  82.     Remove_FormDLL
  83.     MsgBox "Ok"
  84.     Exit Sub
  85. Ie_Err:
  86.     Application.Wait Time + #12:00:05 AM#                '等候網頁
  87.     Set Table = IE.document.getElementsByTagName("TABLE")
  88.     Resume
  89. End Sub
  90. Private Sub Date_of_refresh(ByVal Code As String, ByVal xPage As Integer) '導入資料程式
  91.     Dim AR(), i As Long, S As Variant, Sy As String, Ta As String
  92.     Dim D As New DataObject, SH As Worksheet
  93.     On Error GoTo Sh_Err
  94.     With CreateObject("Scripting.FileSystemObject").OpenTextFile(Code_txt)
  95.         Ta = .Readall
  96.         .Close
  97.     End With
  98.     With D
  99.         .SetText Ta
  100.         .PutInClipboard
  101.     End With
  102.     With ThisWorkbook.Sheets(Code)
  103.         .Range("a1").PasteSpecial
  104.         If xPage > 1 Then
  105.             With .Range("A:A").SpecialCells(xlCellTypeConstants).Offset(1)
  106.                 .Replace "Date", "=xxx", xlWhole
  107.                 .SpecialCells(xlCellTypeFormulas, xlErrors).EntireRow.Delete
  108.             End With
  109.         End If
  110.         AR = .Range("A:A").SpecialCells(xlCellTypeConstants).Value
  111.         AR = Application.Transpose(AR)
  112.          '日期整理 ***************
  113.         For i = 2 To UBound(AR)
  114.             S = Split(AR(i), "/")
  115.             Sy = "20"
  116.             If Val(S(2)) > Mid(Year(Date), 3) Then Sy = "19"
  117.             If Len(S(0)) = 2 Then
  118.                 S = Sy & S(2) & "/" & S(0) & "/" & S(1)
  119.                 ElseIf Len(S(0)) = 4 Then
  120.                 S = Sy & S(2) & "/" & Mid(S(0), 3) & "/" & S(1)
  121.             End If
  122.             AR(i) = S
  123.         Next
  124.         .Range("A:A").SpecialCells(xlCellTypeConstants).Value = Application.Transpose(AR)
  125.         '*****************************
  126.         Application.Goto .Range("A1")
  127.         
  128.     End With
  129.     Exit Sub
  130. Sh_Err:
  131.     If Err = 9 Then
  132.         ThisWorkbook.Sheets.Add.Name = Code
  133.         Err.Clear
  134.     End If
  135.     On Error GoTo 0
  136.     Resume
  137. End Sub
  138. Private Sub Set_FormDLL()   '新增引用 Microsoft Forms 2.0 Object Library
  139.     On Error Resume Next
  140.     ThisWorkbook.VBProject.References.AddFromFile "C:\windows\system32\" & FormDLL
  141. End Sub
  142. Private Sub Remove_FormDLL() '刪除引用 Microsoft Forms 2.0 Object Library
  143.     Dim D As Object
  144.     For Each D In ThisWorkbook.VBProject.References
  145.         If UCase(D.fullpath) Like "*" & FormDLL Then
  146.             ThisWorkbook.VBProject.References.Remove D
  147.         End If
  148.     Next
  149. End Sub
  150. Private Sub 網頁的元素()
  151.     Dim URL As String, A As Object, i As Integer
  152.     URL = "http://www.cnyes.com/USAstock/history.aspx?code=sgen"
  153.     With CreateObject("InternetExplorer.Application")
  154.        ' .Visible = True     '  是否顯示 IE
  155.         .Navigate URL
  156.         Do While .readyState <> 4
  157.             DoEvents
  158.         Loop
  159.         Set A = .document.all
  160.         On Error Resume Next
  161.         With ActiveSheet
  162.             .Cells.Clear
  163.             For i = 0 To A.Length - 1
  164.                 .Cells(i + 1, "a") = A(i).tagname
  165.                 .Cells(i + 1, "b") = A(i).ID
  166.                 .Cells(i + 1, "c") = A(i).Name
  167.                 .Cells(i + 1, "d") = A(i).Type
  168.                 .Cells(i + 1, "e") = A(i).Value
  169.                 .Cells(i + 1, "f") = A(i).innerText
  170.                 .Cells(i + 1, "g") = A(i).class
  171.                  .Cells(i + 1, "g") = A(i).class
  172.             Next
  173.         End With
  174.         .Quit
  175.     End With
  176. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

不好意思 我可能需要版主您提點我一下
圖片中 那個日期有一個是下拉 其它二個是用填值的 我不知這種的怎麼寫
我會寫一個後 就會改成用迴圈來抓取我需要的值了

SGEN.jpg (137.23 KB)

SGEN.jpg

小菜鳥一個 請多指教~

TOP

感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

我看 length =1 剛自己改了程式 試跑 好像不會停了
等睡前整個丟下去抓十萬筆資料試試
感謝~
小菜鳥一個 請多指教~

TOP

本帖最後由 GBKEE 於 2014-6-5 09:04 編輯

回復 6# kasl
但我比較好奇的是 我以為前面那個 do while loop 會幫我做把關的動作,原來沒有。

這網頁下載流量速度的因素
  
我有用F8單步在那看,有時是網頁打開的速度太慢,

修改一下試試看
程式正常時  A.Length = ?
  1. xlHtm = .Document.body.innerHTML                '儲存
  2.   'Set A = Nothing
  3.   Do
  4.     Set A = .Document.getElementsByTagName("table")
  5.   Loop Until A.Length >= ? And Not A Is Nothing
  6.   .Document.body.innerHTML = A(0).outerHTML
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

Set A = Nothing
  Do While A Is Nothing  '等候網頁下載資料完成
    Set A = .Document.getElementsByTagName("table")
  Loop
  .Document.body.innerHTML = A(0).outerHTML
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
當我用 loop狂抓網頁資料時發現有時還是會卡在這一行。
我有用F8單步在那看,有時是網頁打開的速度太慢,但我比較好奇的是
我以為前面那個 do while loop 會幫我做把關的動作,原來沒有。
小菜鳥一個 請多指教~

TOP

原來是這樣判斷 我懂了
感謝~
小菜鳥一個 請多指教~

TOP

回復 3# kasl

   
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 他三不五時會停在這行
說找不到定義的物件? 不過我過段時間再按F5就又會跑了
程式執行的速度,比網頁下載資料速度快了
  1. With CreateObject("InternetExplorer.Application")
  2.   .Visible = True     '  是否顯示 IE
  3.   .Navigate URL
  4.   Do While .ReadyState <> 4 Or .Busy
  5.     DoEvents
  6.   Loop
  7.   xlHtm = .Document.body.innerHTML                '儲存
  8.   Set A = Nothing
  9.   Do While A Is Nothing  '等候網頁下載資料完成
  10.     Set A = .Document.getElementsByTagName("table")
  11.   Loop
  12.   .Document.body.innerHTML = A(0).outerHTML
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

Sub Purchases()

Set shts = ActiveSheet
   
For i = 0 To 6000 Step 50
   
If i = 0 Then
  j = 1
Else
  j = (i / 50) * 51 + 1
End If
   
URL = "http://www.insidermonkey.com/insider-trading/screener/#/offset=" & i & "&symbol=&price=&filing-date-from=&filing-date-to=2014-05-23&transaction=All&amount=&title-director=false&title-officer=false&title-large-shareholder=false&title-other=false&min-transaction-amount=1000000&number-of-min-insiders="
   
With CreateObject("InternetExplorer.Application")
  .Visible = False     '  是否顯示 IE
  .Navigate URL
         
         
  Do While .ReadyState <> 4 Or .Busy
    DoEvents
  Loop
  
  Do While .ReadyState <> 4 Or .Busy
    DoEvents
  Loop
      
  xlHtm = .Document.body.innerHTML                '儲存
  Set A = .Document.getElementsBytagname("table")

  .Document.body.innerHTML = A(0).outerHTML
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 他三不五時會停在這行
說找不到定義的物件? 不過我過段時間再按F5就又會跑了
請問這個怎麼改進
  
  .ExecWB 17, 2       '  Select All
  .ExecWB 12, 2       '  Copy selection
            
  With shts
    .Cells(j, 1).Select
    .PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True
  End With
            
  .Document.body.innerHTML = xlHtm                  '還原

  shts.Cells.EntireColumn.AutoFit     '  自動調整欄寬
        
  .Quit
        
End With
   
Next i
   
End Sub
小菜鳥一個 請多指教~

TOP

        靜思自在 : 盡多少本份,就得多少本事。
返回列表 上一主題