返回列表 上一主題 發帖

[發問] 網頁資料很長,如何有效率地知道已完整進來

[發問] 網頁資料很長,如何有效率地知道已完整進來

本帖最後由 Scott090 於 2017-11-12 17:01 編輯

'捲動網頁
'''' i 值太小,網頁資料會來不及進來;太大會浪費時間
''''   有何方法可以知道資料已進來齊全而停止;或其他方式取代本方法?
            i = 0
            While i < 50
                .Document.Parentwindow.scrollby 0, 10000 'Move,Scroll IE window down to bottom
                i = i + 1
            Wend

請問那一位前輩幫忙? 先謝謝

以下的例子是資料的長短多寡取決於日期期間的長短,如何做會比較有效率?
  1. '使用 ie,Application 物件
  2. '從 finance.Yahoo.com取得歷史資料
  3. '
  4. Sub getHistoricalData()
  5.         Dim Code
  6.         Code = "AAPL"
  7.         Const mysteryNum = 2209190400#
  8.         Dim ie, DATAar, A$, yyDate
  9.         Dim URL As String
  10.         Dim StartDate, EndDate, timer, tt As Date
  11.         Dim Table As Object, oDoc As Object
  12.         Dim i%, j%, k%
  13.         
  14.         StartDate = "1999/1/2"            '開始日期
  15.         EndDate = Date                          '結束日期,預設為今天

  16.       '轉換為秒鐘數字
  17.     StartDate = DateValue(StartDate) * 86400 - mysteryNum
  18.     EndDate = DateValue(EndDate) * 86400 - mysteryNum
  19.    
  20.    Set ie = CreateObject("InternetExplorer.Application")
  21.         
  22.     With ie
  23.             Application.StatusBar = "打開網頁,等待資料備齊 ....."
  24.             URL = "https://finance.yahoo.com/quote/" & Code & "/history?period1=" & StartDate & "&period2=" & EndDate & "&interval=1d&filter=history&frequency=1d"
  25.             .Visible = True ' False                '顯示 IE否?
  26.             .navigate URL
  27.             Application.Wait Time + #12:00:04 AM#                   '等候網頁4秒鐘
  28.             tt = Time + #12:00:05 AM#
  29.             Do While (.busy Or .readyState <> 4) And Time < tt
  30.                 DoEvents
  31.             Loop
  32.             
  33.             '捲動網頁
  34. '''' i 值太小,網頁資料會來不及進來;太大會浪費時間
  35. ''''   有何方法可以知道資料已進來齊全而停止;或其他方式取代本方法?
  36.             i = 0
  37.             While i < 50
  38.                 .Document.Parentwindow.scrollby 0, 10000 'Move,Scroll IE window down to bottom
  39.                 i = i + 1
  40.             Wend
  41.                         
  42.             Application.StatusBar = "取網頁資料中 .... "
  43.             Set oDoc = .Document.getElementsByTagName("TABLE")(1)
  44.             
  45.             ActiveSheet.Cells.Clear
  46.             ReDim DATAar(oDoc.Rows.Length - 1, 7)
  47.             
  48.             For i = 0 To oDoc.Rows.Length - 2
  49.                     For j = 0 To 6
  50.                           On Error Resume Next
  51.                           DATAar(i + 1, j + 1) = oDoc.Rows(i).Cells(j).innertext
  52.                           If j = 0 And i <> 0 Then
  53.                           
  54.                           ''''    yyDate = CDate("Oct 01, 2017")
  55.                                 A = DATAar(i + 1, j + 1)
  56.                                 A = Mid(A, 2, 3) & " " & Mid(A, 8, 2) & ", " & Right(A, 4)
  57.                                 yyDate = CDate(A)
  58.                                 DATAar(i + 1, j + 1) = yyDate
  59.                                 
  60.                           End If
  61.                     Next
  62.             Next
  63.             Application.StatusBar = "資料移到Excel 工作表 ....."
  64.             ActiveSheet.Cells(1, 1).Resize(UBound(DATAar), 7).Value = DATAar
  65.             Columns("A:A").NumberFormatLocal = "yyyy/mm/dd"
  66.             Columns("G:G").NumberFormatLocal = "#,##0_)"
  67.             .Quit
  68.     End With
  69.     Application.StatusBar = "資料下載完成 ....... "
  70.     Application.Wait Time + #12:00:02 AM#
  71.     Application.StatusBar = False
  72.    
  73. End Sub
複製代碼

本帖最後由 Scott090 於 2017-11-13 16:01 編輯

回復 2# GBKEE


  謝謝  GBKEE 大的指導
為觀察網頁行為特性將兩種方法做一比較,執行結果如下表:
設定欲取資料期間;開始 1999/1/2, 結束期間 2017/9/12       


其中方法二並未完整的取得資料;
方法一則視 scrollby 的參數而定,設小了取資料不完整,設大了資料可以完整取得但有空轉的時間
謝謝
  1. Sub Ex_網頁底部()
  2.     Dim E As Object, R As Long, C As Long, i%
  3.     Dim timer As Date
  4.     'period1 = 1483286400     '2017/1/2
  5.     'period1 = 1451664000    '2016/1/2
  6.     'period1 = 915206400      '1999/1/2
  7.     'period2 = 1505174400     '2017/9/12

  8.     With CreateObject("InternetExplorer.Application")
  9.         .Visible = True
  10.         .Navigate "https://finance.yahoo.com/quote/AAPL/history?period1=915206400&period2=1505174400&interval=1d&filter=history&frequency=1d"
  11.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  12.         Set E = .document.ALL.TAGS("TR")
  13.        i = 0
  14.        timer = Time
  15.         Do
  16.             i = i + 1
  17.            With .document
  18. '方法一:
  19. '                .Parentwindow.scrollBy 0, 500  '改變 y 點數做試驗
  20. '                With .DocumentElement
  21. '                    R = R + .clientheight
  22. '                    Debug.Print "捲動次數:" & i, "R:" & R, "SH: " & .scrollHeight ', "CH: " & .clientheight
  23. '                    If R - .clientheight > .scrollHeight Then Exit Do
  24. '                End With
  25. '==============================================================
  26. '方法二:
  27.                   .Parentwindow.scrollBy 0, .DocumentElement.scrollHeight
  28.                   R = R + .DocumentElement.scrollHeight
  29.                   Debug.Print "捲動次數:" & i, "R:" & R, "SH: " & .DocumentElement.scrollHeight
  30.                   If R - .DocumentElement.scrollHeight > .DocumentElement.scrollHeight Then Exit Do
  31. ''''==============================================================
  32.                   
  33.             End With
  34.             DoEvents
  35.         Loop
  36.         
  37.         Debug.Print "時程: " & (Time - timer) * 86400
  38.         
  39.         ActiveSheet.Cells.Clear
  40.         For R = 1 To E.Length - 1
  41.             For C = 0 To E(R).Cells.Length - 1
  42.                 Cells(R, C + 1) = E(R).Cells(C).INNERTEXT
  43.             Next
  44.         Next
  45.         .Quit        '關閉網頁
  46.     End With
  47.     MsgBox "Ok"
  48. End Sub
複製代碼

TOP

回復 4# GBKEE


    GBKEE大:
謝謝進一步的解答與教導 及提供的參考的網址
將繼續地瞭解與研究

感恩

TOP

回復 4# GBKEE

從參考的網址內的各個變數,  
scrollHeight,  scrolltop,  offsetheight, clientHeight, body.clientheight .....
沒找出確實的 滾動數字相互之間的穩定關係,造成 激發滾動迴圈的不穩定性 及 取得的資料不一定 是底部最後一筆;
而,欲取得的資料格式是可以既知,所以改用 "是否最後一筆資料" 來決定滾動迴圈

把程式碼修飾如下:
增加 日期 變數 Dim startDate as date,  A

startdate = #1999/1/2#

'跳出迴圈條件 If .scrolltop + .clientHeight = .scrollHeight Then exit do 改成
A = E(E.Length - 2).Cells(0).INNERTEXT
If CDate(Mid(A, 2, 3) & " " & Mid(A, 8, 2) & ", " & Right(A, 4)) - startDate < 5 Then Exit Do

測試結果是穩定的

TOP

回復 7# GBKEE


    GBKEE 大大:
謝謝你的總結

TOP

回復 8# quickfixer


   
我在01文書處理區有看到只要1.8秒的程式碼


請把網址貼出來,讓大家參考
謝謝

TOP

回復 11# quickfixer


參考網址:   https://www.mobile01.com/topicdetail.php?f=511&t=4737630&p=28
這個是直接下載 .csv 檔
謝謝提供的信息

TOP

回復 13# quickfixer


    這個麻辣家族論壇也有使用 xmlhttp 供參考的例子,
    搜索一下就有了

TOP

回復 15# quickfixer


    謝謝提供的信息
去年有去瀏覽過

TOP

        靜思自在 : 是非當教育,讚美作警惕。
返回列表 上一主題