返回列表 上一主題 發帖

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

回復 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

本帖最後由 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

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

TOP

回復 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

        靜思自在 : 吃苦了苦、苦盡廿來,享福了福、福盡悲來。
返回列表 上一主題