返回列表 上一主題 發帖

[發問] 請問這可設定等幾秒沒開網頁或網頁錯誤就執行下一筆嗎,謝謝

[發問] 請問這可設定等幾秒沒開網頁或網頁錯誤就執行下一筆嗎,謝謝

Private Sub GetDividend(ByVal ss As String)
    Dim rr As String

   rr = "http://yamstock.megatime.com.tw/asp/stockinfo/GetReport.asp?select_table=html\Ficxxx\&stockid=2412&" & ss & "&name1=D4&index1=12"     ' 正確的
    工作表2.Select
    Cells.Clear

    Set ie = CreateObject("internetexplorer.application") '使用此方式可以免除 "設定引用項目"

    With ie
        .Visible = False 'True為開啟ie, False為不開啟ie
        .Navigate rr
        Do While .ReadyState <> 4 '等待網頁開啟
        DoEvents
        Loop
        .ExecWB 17, 2 'Select All
        .ExecWB 12, 2 'Copy selection
        
        工作表2.Range("A1").Activate
        
        ActiveSheet.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:= _
            False, NoHTMLFormatting:=True
    End With

     ie.Quit
   End Sub

Sub AllFile()
Dim i As Integer, v

On Error Resume Next
For i = 2 To 工作表1.Range("A" & 工作表1.Rows.Count).End(xlUp).Row

v = 工作表1.Cells(i, 1).Value

Call GetDividend(v)
工作表1.Cells(i, 9).Value = 工作表2.Cells(34, 8).Value
Debug.Print 工作表1.Cells(i, 1).Value & " " & 工作表1.Cells(i, 2).Value

Next

End Sub

GetDividend.rar (72.65 KB)

回復 28# quickfixer


    謝謝 大大
修改
  1. Sub test()
  2. Debug.Print Date
  3. '修改1月的前一個月便0
  4. If Split(Date, "/")(1) - 1 = 0 Then
  5. Debug.Print 12
  6. Else
  7. Debug.Print Split(Date, "/")(1) - 1
  8. End If

  9. End Sub
複製代碼

TOP

本帖最後由 wufonna 於 2022-2-13 17:20 編輯

回復 28# quickfixer


    謝謝 大大
之前程式可跑完,可能這兩天營收更新,網頁才有缺少資料,等網頁更新完在下載看看。
加了程式碼程式有跑完。

Image 1.jpg (145.4 KB)

Image 1.jpg

TOP

本帖最後由 quickfixer 於 2022-2-12 23:14 編輯

回復 27# quickfixer

我參考你給的01網址686f,加入重新下載功能,可全部跑完
  1. Private Sub GetDividend(ByVal ss As String)     '取股利網頁 '2022/2/22 換這段程式碼 在 https://www.mobile01.com/topicdetail.php?f=511&t=4737630&p=3 的21樓
  2. Dim URL, HTMLsourcecode, GetXml, Table
  3. Dim i As Integer, j As Integer, r As Integer
  4. Set HTMLsourcecode = CreateObject("htmlfile")
  5. Set GetXml = CreateObject("msxml2.xmlhttp")
  6. URL = "http://pscnetinvest.moneydj.com.tw/z/zc/zch/zch_" & ss & ".djhtm"

  7. r = 0
  8. retry:
  9. On Error Resume Next

  10. With GetXml
  11. .Open "GET", URL, False
  12. .setRequestHeader "Cache-Control", "no-cache"
  13. .setRequestHeader "Pragma", "no-cache"
  14. .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
  15. .send

  16. HTMLsourcecode.body.innerhtml = .responsetext
  17. Set Table = HTMLsourcecode.all.tags("table")(2).Rows

  18. If Err.Number <> 0 Then

  19. If r > 3 Then
  20. '超過3次,改抓下一筆,避免無限loop
  21. Exit Sub
  22. End If
  23. Debug.Print Err.Description
  24. r = r + 1
  25. On Error GoTo -1
  26. Err.Clear
  27. '等0.5秒
  28. Delaytick (0.5)
  29. GoTo retry

  30. End If
  31.             
  32. For i = 0 To Table.Length - 1
  33. For j = 0 To Table(i).Cells.Length - 1
  34. 工作表2.Cells(i + 1, j + 1) = Table(i).Cells(j).innertext
  35. Next j
  36. Next i
  37. End With
  38. Set HTMLsourcecode = Nothing
  39. Set GetXml = Nothing
  40. End Sub



  41. Sub Delaytick(setdelay As Single)
  42.    
  43.     Dim StartTime As Double, NowTime As Double
  44.     StartTime = Timer * 100
  45.     setdelay = setdelay * 100
  46.     Do
  47.       NowTime = Timer * 100
  48.       DoEvents
  49.     Loop Until NowTime - StartTime > setdelay
  50.    
  51. End Sub
複製代碼
但是你allfile裡面那個Split(Date, "/")(1) - 1
可能要改一下,有人excel日期是用-不是/
用mid可能會比較好一些

TOP

本帖最後由 quickfixer 於 2022-2-12 22:45 編輯

回復 26# wufonna

沒注意到SelectionChange裡面有重抓的程式碼

出錯時    debug.print HTMLsourcecode.body.innerhtml 出現這個,沒有抓到資料


google httpcode=500
伺服器端錯誤回應
500 Internal Server Error
伺服器端發生未知或無法處理的錯誤。

可能程式跑太快,同個ip請求太多,網頁來不及處理

TOP

回復 25# quickfixer


    謝謝 大大 ,我有用
  1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  2.     Application.EnableEvents = False
  3.         If Target(1).Column = 1 And Target(1).Address(0, 0) <> "A1" Then          '在第1列
  4.             If Target(1).Value <> "" Then
  5.             
  6. '         MsgBox Target(1).Address(0, 0) & vbCrLf & vbCrLf & Target(1).Value & vbCrLf & vbCrLf & Target(1).Row
  7.             Call MyFile(Target(1).Value, Target(1).Row)
  8.             End If
  9.         End If
  10.     Application.EnableEvents = True

  11. End Sub
複製代碼
抓取個別的資料
想請教有程式取代
On Error Resume Next '下行會出錯,加入這行,未知原因。
這段程式碼嗎

TOP

本帖最後由 quickfixer 於 2022-2-12 21:01 編輯

回復 24# wufonna


    玩了一下,會出錯是沒抓到資料,好像是程式跑太快,流量限制的問題,可是沒擋ip?
程式沒問題,另外做一個commandbutton,全部編號跑完後,再重抓有空白的資料
Sub test()
    With 工作表1
        For i = 2 To .Range("A" & .Rows.Count).End(xlUp).Row
只抓沒資料的編號
           If .Cells(i, 3) = "" Then
            v = .Cells(i, 1).Value
            GetDividend (v)
'這幾行code 同 AllFile ,恕刪
           End If
        Next
    End With
End Sub

TOP

回復 1# wufonna
改了程式內容

請教程式沒加入這段 On Error Resume Next '下行會出錯,加入這行,未知原因。 會錯誤是網頁有空格的關溪嗎?
請教大大如何修改 謝謝
  1. Option Explicit
  2. Dim ie As Object   '模組最頂端 Dim 供這模組的程序使用的變數
  3. Sub AllFile()
  4.     Dim i As Integer, v, Y As Integer, S As String
  5.     Dim z As Integer

  6.     With 工作表1
  7.         Dim AR
  8.            AR = .Range("C1:J1")
  9.           .Range("C:J") = ""
  10.           .Range("C1:J1") = AR
  11.           z = 0
  12.         For i = 2 To .Range("A" & .Rows.Count).End(xlUp).Row
  13.      
  14.            v = .Cells(i, 1).Value
  15.             GetDividend (v)
  16.              .Cells(i, 3).Resize(1, 7).Value = 工作表2.Cells(7, 1).Resize(1, 7).Value

  17.              If 工作表2.Cells(7, 5).Value > 0 Then
  18.                .Cells(i, 10).Value = 1
  19.                z = z + 1
  20.               Else
  21.                .Cells(i, 10).Value = 0
  22.               End If
  23.                             If 工作表2.Cells(7, 5).Value > 0 And 工作表2.Cells(8, 5).Value > 0 And 工作表2.Cells(9, 5).Value > 0 Then 'K(營收連3個月正成長)
  24.                 .Cells(i, 11).Value = 1
  25.               Else
  26.                 .Cells(i, 11).Value = 0
  27.               End If
  28.         Next
  29. '            MsgBox "共有" & z & "家正成長"
  30. .Cells(1, 10).Value = "去年同期年增率" & Split(Date, "/")(1) - 1 & "月份" & .Range("A" & .Rows.Count).End(xlUp).Row & "家共有" & z & "家正成長"
  31.    
  32.     End With

  33. End Sub

  34. Public Function MyFile(v As Integer, i As Integer)
  35.   '   Dim i As Integer, v, Y As Integer, S As String

  36.     With 工作表1
  37.            .Range("C" & v & ":J" & v) = "" '清除工作表1,年度範圍
  38.            v = .Cells(i, 1).Value
  39.             GetDividend (v)
  40.              .Cells(i, 3).Resize(1, 7).Value = 工作表2.Cells(7, 1).Resize(1, 7).Value

  41.              If 工作表2.Cells(7, 5).Value > 0 Then
  42.                .Cells(i, 10).Value = 1

  43.               Else
  44.                .Cells(i, 10).Value = 0
  45.               End If
  46.               If 工作表2.Cells(7, 5).Value > 0 And 工作表2.Cells(8, 5).Value > 0 And 工作表2.Cells(9, 5).Value > 0 Then 'k (營收連3個月正成長)
  47.                 .Cells(i, 11).Value = 1
  48.               Else
  49.                 .Cells(i, 11).Value = 0
  50.               End If
  51.               
  52.     End With

  53. End Function



  54. Private Sub GetDividend(ByVal ss As String)     '取股利網頁 '2022/2/22 換這段程式碼 在 https://www.mobile01.com/topicdetail.php?f=511&t=4737630&p=3 的21樓
  55. Dim URL, HTMLsourcecode, GetXml, Table
  56. Dim i As Integer, j As Integer
  57. Set HTMLsourcecode = CreateObject("htmlfile")
  58. Set GetXml = CreateObject("msxml2.xmlhttp")
  59. URL = "http://pscnetinvest.moneydj.com.tw/z/zc/zch/zch_" & ss & ".djhtm"
  60. With GetXml
  61. .Open "GET", URL, False
  62. .setRequestHeader "Cache-Control", "no-cache"
  63. .setRequestHeader "Pragma", "no-cache"
  64. .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
  65. .send

  66. HTMLsourcecode.body.innerhtml = .responsetext
  67. On Error Resume Next '下行會出錯,加入這行,未知原因。
  68. Set Table = HTMLsourcecode.all.tags("table")(2).Rows
  69. For i = 0 To Table.Length - 1
  70. For j = 0 To Table(i).Cells.Length - 1
  71. 工作表2.Cells(i + 1, j + 1) = Table(i).Cells(j).innertext
  72. Next j
  73. Next i
  74. End With
  75. Set HTMLsourcecode = Nothing
  76. Set GetXml = Nothing
  77. End Sub
複製代碼

Annualrate-2022.rar (105.34 KB)

TOP

回復 22# GBKEE

感謝 GBKEE 大
程式跑了幾次都能捉到
程式碼我研究看看 不會再向 GBKEE 大大請教
謝謝 ^_^

TOP

本帖最後由 GBKEE 於 2014-9-4 16:25 編輯

回復 21# wufonna
試試看
  1. Option Explicit
  2. Dim ie As Object   '模組最頂端 Dim 供這模組的程序使用的變數
  3. Dim AR()
  4. Sub AllFile()
  5.     Dim i As Integer
  6.     Set ie = CreateObject("internetexplorer.application")   '使用此方式可以免除 "設定引用項目"
  7.     With 工作表1
  8.         AR = .Range("E1:G1")
  9.         .Range("E:G") = ""
  10.         .Range("E1:G1") = AR
  11.         For i = 2 To .Range("A" & .Rows.Count).End(xlUp).Row
  12.            ReDim AR(1 To 3)
  13.            Application.StatusBar = .Cells(i, 1) & "  " & .Cells(i, 2) & " 讀取中..."
  14.             GetDividend .Cells(i, 1), 3
  15.             GetDividend .Cells(i, 1), 2
  16.             .Range("E1:G1").Offset(i - 1) = AR
  17.         Next
  18.     End With
  19.     ie.Quit
  20. End Sub
  21. Private Sub GetDividend(ByVal ss As String, ByVal table As Integer)
  22.     Dim rr As String, S As Object
  23.     If table = 3 Then
  24.         rr = "http://dj.mybank.com.tw/z/zc/zcc/zcc_" & ss & ".asp.htm"                '股利網頁
  25.     ElseIf table = 2 Then
  26.         rr = "https://djinfo.cathaysec.com.tw/Z/ZC/ZCX/ZCXNEWCATHAYSEC.DJHTM?A=" & ss '收盤價網頁
  27.     End If
  28.     With ie
  29.         .Navigate rr
  30.         Do While .readyState <> 4 Or .Busy                        '等待網頁下載完畢
  31.               DoEvents
  32.         Loop
  33.         With .document.BODY
  34.            If InStr(.INNERTEXT, "個股代碼錯誤") Or InStr(.INNERTEXT, "無此股票資料") Then
  35.                 MsgBox .INNERTEXT
  36.                 Exit Sub
  37.            End If
  38.         End With
  39.         Do
  40.         Set S = .document.getElementsByTagName("table")(table)   ' 新的 table 4
  41.         Loop Until Not S Is Nothing
  42.         If table = 3 Then
  43.             AR(1) = S.Rows(1).Cells(1).INNERTEXT            '現金股利
  44.             AR(2) = S.Rows(1).Cells(4).INNERTEXT            '股票股利
  45.         ElseIf table = 2 Then
  46.             AR(3) = S.Rows(1).Cells(7).INNERTEXT            '收盤價
  47.         End If
  48.     End With
  49. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 為自己找藉口的人永遠不會進步。
返回列表 上一主題