返回列表 上一主題 發帖

[發問] 用Selenium 抓上市行情即時資料 ???

本帖最後由 singo1232001 於 2022-10-24 12:59 編輯

回復 32# t8899


加入了自動更新selenium chromedriver的功能

    https://drive.google.com/drive/folders/1usKHGwtAZ1oJENT-DYzwrZH90HOo_6TV?usp=sharing
    (舊版的chromedrive.exe  測試更新會卡住狀況) 檔案過大 丟到雲端


另外 盤中高峰期 它們api 可能會會出現 卡超過15~20秒的情況
也可能是短期間 快速查詢(940/140 = 7次) 造成api異常
可能會被暫時ban IP 建議不要940全跑 或者 短期間跑太多次

seleniumdownload v7.zip (83.39 KB)

TOP

回復 33# singo1232001

seleniumdownload v8.zip (277.67 KB)

TOP

回復 34# singo1232001


    再優化一下

seleniumdownload v9.zip (304.84 KB)

TOP

回復 38# t8899


    這網站api好像有2種限制

1種是次數限制  我用兩支股票 手動複製網址 大概連續秒查30次(不到一分鐘) 就被踢
1種是數量限制  940隻(6~7次) 可能要40秒才能做一次  懷疑是 一分鐘限制查8次 或者 一分鐘內 限制不能超過1000隻 (會累計扣掉剩餘用量)

TOP

本帖最後由 singo1232001 於 2022-10-28 18:42 編輯

回復 42# t8899


用bytag 而且這網頁我之前玩過

另外就是上面提到的 這網頁後面不會一次跑出來
只能用tag 跟畫面移動控制 來讓後面的跑出來
他的項目不是一般table 而是用class跑出來

需要用 find..s..tag+判斷+迴圈+等待 存在來逐步向內找更內層的元素 有點莫名的像是檔案總管一直開子資料夾那樣


檔案是5.6月左右寫得,能不能動 我不太清楚 也沒去修改

網頁爬蟲v1.zip (52 KB)

TOP

本帖最後由 singo1232001 於 2023-4-17 01:51 編輯

回復 45# Scott090


已測試 檔案沒問題
需要設定防毒 新增排除項目 把這個檔案排除在防毒之外

剛做了交叉測試

應該是下面這段sub 因為有調用到shell(CMD)  自動執行 下載+跟解壓縮+刪檔 就被被判定惡意程序
Sub updataSelenium()  '自動更新selenium
path1 = "C:\Users\" & Environ$("username") & "\AppData\Local\SeleniumBasic\Chromedriver.exe"
path2 = "C:\Program Files\SeleniumBasic\chromedriver.exe"
If Dir(path1) <> "" Then TempDrvFile = path1
    'Kill (TempDrvFile)  '假如在錯誤的情況下 找到了檔案 那之後將要更新複製覆蓋 避免跳出複製已存在錯誤警告 所以先刪除掉
If Dir(path2) <> "" Then TempDrvFile = path2
foler = Left(TempDrvFile, InStrRev(TempDrvFile, "\"))


Set objHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")  'Get chrome version
chrversion = CreateObject("WScript.Shell").RegRead("HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon\version")
dotsarr = Split(chrversion, ".")
leftchrver = dotsarr(0) & dotsarr(1)


Set oShell = CreateObject("wscript.shell")
errcode = oShell.Exec(TempDrvFile & " --version").StdOut.ReadAll
verarr = Split(errcode, " ")
chrdrv = verarr(1)
dotsarr2 = Split(chrdrv, ".")
leftchrdrv = dotsarr2(0) & dotsarr2(1)
If leftchrver = leftchrdrv Then Exit Sub


Url = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_" & dotsarr(0)
Call objHttp.Open("GET", Url, False)
Call objHttp.Send("")
version_number = objHttp.responseText
dotsarr3 = Split(version_number, ".")
leftversion_no = dotsarr3(0) & dotsarr3(1)
download_url = "https://chromedriver.storage.googleapis.com/" + version_number + "/chromedriver_win32.zip"
   
    Call objHttp.Open("GET", download_url, False)
    Call objHttp.Send("")
        

        Set fileStream = CreateObject("ADODB.Stream")
        With fileStream
            .Open
            .Type = 1 'adTypeBinary
            .Write objHttp.responseBody
            .Position = 0
            .SaveToFile foler & "chromedriver.zip", 2 '                   "C:\Program Files\SeleniumBasic\chromedriver.zip", 2  'adSaveCreateOverWrite 'adSaveCreateOverWrite
            .Close
        End With
        
        '解壓縮        'C:\Program Files\SeleniumBasic
        On Error GoTo 0
        Set oApp = CreateObject("Shell.Application")
        If Dir(TempDrvFile) <> "" Then Kill TempDrvFile
        oApp.Namespace(foler).CopyHere _
            oApp.Namespace(foler & "chromedriver.zip").items

End Sub

上面這段是 查詢google chrome更新版本號
再從selenium官網 下再最新更新檔
自動更新chromedriver.exe更新的自動程序
並且刪除舊的chromedriver.exe
沒這段 就必須手動更新

也可以測試
若直接把這份檔案 丟到google雲端 開共享 刷新 也會跳旗子(有威脅性檔案)
如果你複製一份檔案 把上面那段都刪了 再丟到google雲端 就沒反應了

補充
若是下載後 打開不執行的情況 又是另一種情況
檔案>右鍵>內容> 解除封鎖

TOP

本帖最後由 singo1232001 於 2023-8-20 19:47 編輯

回復 49# Scott090

本人不是很專業 自己也沒把握可以100%沒問題
加上不知道之後有那些網址與語法是固定的
所以可以先將就用看看

    Sub updataSelenium()
'由於selenium有可能會安裝在windows下兩個其中一個資料夾中 所以要先找出資料夾位置 (mac目前未考慮)
path1 = "C:\Users\" & Environ$("username") & "\AppData\Local\SeleniumBasic\Chromedriver.exe"
path2 = "C:\Program Files\SeleniumBasic\chromedriver.exe"
If Dir(path1) <> "" Then TempDrvFile = path1
If Dir(path2) <> "" Then TempDrvFile = path2
foler = Left(TempDrvFile, InStrRev(TempDrvFile, "\")) '取得 資料夾路徑 與 chromedrive.exe 路徑

'獲取當前chromedriver版本前兩碼 例: 117.0
Set oShell = CreateObject("wscript.shell")
errcode = oShell.Exec(TempDrvFile & " --version").StdOut.ReadAll
verarr = Split(errcode, " ")
chrdrv = verarr(1)
dotsarr2 = Split(chrdrv, ".")

'獲取chrome當前瀏覽器版本號     例: 117.0
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")  'Get chrome version
chrversion = CreateObject("WScript.Shell").RegRead("HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon\version")
dotsarr = Split(chrversion, ".")
leftchrver = dotsarr(0) & dotsarr(1)

'比對chromedriver 與 chrome瀏覽器 兩個版本號, 相同(不更新) ; 不同(上官網查瀏覽器最新版本號 並繼續下載解壓取代)
leftchrdrv = dotsarr2(0) & dotsarr2(1)
If leftchrver = leftchrdrv Then Exit Sub

'到官網api獲取版本資訊 並拆解資訊 該資訊為json格式 並獲得當前瀏覽器可用的chromedriver版本號
'本json拆法為特製 不符合正規json 不知道未來的格式會不會變 因為不想引用額外物件
Url = "https://googlechromelabs.github.io/chrome-for-testing/latest-versions-per-milestone.json"
Call objHTTP.Open("GET", Url, False)
Call objHTTP.Send("")
version_number = objHTTP.responseText
v0 = Split(version_number, ",""milestones"":{")
v1 = Split(v0(1), "},")
For i = 0 To UBound(v1)
v1(i) = Replace(v1(i), """", "")
v1(i) = Replace(v1(i), ":{milestone:", ",")
v1(i) = Replace(v1(i), ",version:", ",")
v1(i) = Replace(v1(i), ",revision:", ",")
v1(i) = Replace(v1(i), "}}}", ",")
Next
ReDim v2(UBound(v1))
For i = 0 To UBound(v1): v2(i) = Split(v1(i), ","): Next
For i = 0 To UBound(v2)
If v2(i)(0) = dotsarr(0) Then: version_number = v2(i)(2): Exit For
Next

'生成連結網址  (另須注意選擇作業系統位元 在下方cmd處 也有需要修改的部分)  不知道未來網址會不會變
'download_url = "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/" & version_number & "/linux64/chromedriver-linux64.zip"
'download_url = "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/" & version_number & "/mac-x64/chromedriver-mac-x64.zip"
'download_url = "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/" & version_number & "/win32/chromedriver-win32.zip"
download_url = "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/" & version_number & "/win64/chromedriver-win64.zip"
    Call objHTTP.Open("GET", download_url, False)
    Call objHTTP.Send("")

        Set fileStream = CreateObject("ADODB.Stream")  '使用adodb.stream 接受檔案
        With fileStream
            .Open
            .Type = 1 'adTypeBinary
            .Write objHTTP.responseBody
            .Position = 0
            .SaveToFile foler & "chromedriver.zip", 2 '  下載檔案到selenium資料夾    ' 2  adSaveCreateOverWrite
            .Close
        End With
        
        '使用shell+cmd殺舊的檔案   kill的方式被微軟封了
        Set oApp = CreateObject("Shell.Application")
        cmdCommand = "cmd /c del " & foler & "chromedriver.exe"     '使用cmd刪除檔案 cmd /c del "path"
        Call Shell(cmdCommand, vbHide)                             '殺掉舊的chromedriver.exe
        cmdCommand = "cmd /c del " & foler & "LICENSE.chromedriver"
        Call Shell(cmdCommand, vbHide)                             '殺掉舊的LICENSE.chromedriver
            '若沒殺成功 該程序沒關閉 無法刪除時 將執行佇列中的檔案關閉  'cmd /c taskkill /F /IM chromedriver.exe 是殺佇列
            If Dir(foler & "chromedriver.exe") <> "" Then
            cmdCommand = "cmd /c taskkill /F /IM chromedriver.exe && cmd /c del " & foler & "chromedriver.exe"  '殺掉舊的chromedriver.exe
            Call Shell(cmdCommand, vbHide)
            End If
            If Dir(foler & "LICENSE.chromedriver") <> "" Then
            cmdCommand = "cmd /c taskkill /F /IM LICENSE.chromedriver && cmd /c del " & foler & "LICENSE.chromedriver"  '殺掉舊的LICENSE.chromedriver
            Call Shell(cmdCommand, vbHide)
            End If
            
        '將新的chromedriver.exe解壓縮到資料夾中  16是強制取代選項 注意壓縮檔內的檔名 會因系統位元不同資料夾不同
        oApp.Namespace(foler).CopyHere _
            oApp.Namespace(foler & "chromedriver.zip\chromedriver-win64").items, 16

End Sub

TOP

回復 51# Scott090

由於有人不知道要去安裝主插件 所以也把插件安裝流程寫進去了
https://github.com/florentbr/SeleniumBasic/releases
SeleniumBasic v2.0.9.0.exe
第一次安裝完成後,必須在開始>執行selenium中的start chrome
等待錯誤彈出並確定,將自動安裝.net framwork 安裝完畢後重開機
再執行本程序則可以更新版本


Sub updataSelenium()
'由於selenium有可能會安裝在windows下兩個其中一個資料夾中 所以要先找出資料夾位置 (mac目前未考慮)
path1 = "C:\Users\" & Environ$("username") & "\AppData\Local\SeleniumBasic\Chromedriver.exe"
path2 = "C:\Program Files\SeleniumBasic\chromedriver.exe"
If Dir(path1) <> "" Then TempDrvFile = path1
If Dir(path2) <> "" Then TempDrvFile = path2
foler = Left(TempDrvFile, InStrRev(TempDrvFile, "\")) '取得 資料夾路徑 與 chromedrive.exe 路徑
'若未安裝selenium 2.0.9.0 將會跳轉到github網站 請自行下載安裝
If foler = "" Then
    msg = "(請拍照抓圖本畫面)" & vbCrLf
    msg = msg & "找不到selenium插件資料夾 需至網站" & vbCrLf & "https://github.com/florentbr/SeleniumBasic/releases"
    msg = msg & vbCrLf & "下載安裝 SeleniumBasic v2.0.9.0.exe" & vbCrLf & vbCrLf & "是否直接前往?"
    msg = msg & vbCrLf & vbCrLf & "注意後續步驟:" & vbCrLf & "第一次安裝完成後,必須在開始>執行selenium中的start chrome" & vbCrLf
    msg = msg & "等待錯誤彈出並確定,將自動安裝.net framwork 安裝完畢後重開機" & vbCrLf & "再執行本程序則可以更新版本"
    x = MsgBox(msg, vbYesNo, "未安裝插件")
    If x = 6 Then
    edgePath1 = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
    edgePath2 = "C:\Program Files\Microsoft\Edge\Application\msedge.exe"
    If Dir(edgePath1) <> "" Then edgePath = edgePath1
    If Dir(edgePath2) <> "" Then edgePath = edgePath2
    edgePath = """" & edgePath & """" & " https://github.com/florentbr/SeleniumBasic/releases"
    Call Shell(edgePath, vbNormalFocus)
    End If
Exit Sub
End If

'獲取當前chromedriver版本前兩碼 例: 117.0
Set oShell = CreateObject("wscript.shell")
errcode = oShell.Exec(TempDrvFile & " --version").StdOut.ReadAll
verarr = Split(errcode, " ")
chrdrv = verarr(1)
dotsarr2 = Split(chrdrv, ".")

'獲取chrome當前瀏覽器版本號     例: 117.0
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")  'Get chrome version
chrversion = CreateObject("WScript.Shell").RegRead("HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon\version")
dotsarr = Split(chrversion, ".")
leftchrver = dotsarr(0) & dotsarr(1)

'比對chromedriver 與 chrome瀏覽器 兩個版本號, 相同(不更新) ; 不同(上官網查瀏覽器最新版本號 並繼續下載解壓取代)
leftchrdrv = dotsarr2(0) & dotsarr2(1)
If leftchrver = leftchrdrv Then Exit Sub

'到官網api獲取版本資訊 並拆解資訊 該資訊為json格式 並獲得當前瀏覽器可用的chromedriver版本號
'本json拆法為特製 不符合正規json 不知道未來的格式會不會變 因為不想引用額外物件
Url = "https://googlechromelabs.github.io/chrome-for-testing/latest-versions-per-milestone.json"
Call objHTTP.Open("GET", Url, False)
Call objHTTP.Send("")
version_number = objHTTP.responseText
v0 = Split(version_number, ",""milestones"":{")
v1 = Split(v0(1), "},")
For i = 0 To UBound(v1)
v1(i) = Replace(v1(i), """", "")
v1(i) = Replace(v1(i), ":{milestone:", ",")
v1(i) = Replace(v1(i), ",version:", ",")
v1(i) = Replace(v1(i), ",revision:", ",")
v1(i) = Replace(v1(i), "}}}", ",")
Next
ReDim v2(UBound(v1))
For i = 0 To UBound(v1): v2(i) = Split(v1(i), ","): Next
For i = 0 To UBound(v2)
If v2(i)(0) = dotsarr(0) Then: version_number = v2(i)(2): Exit For
Next

'生成連結網址  (另須注意選擇作業系統位元 在下方cmd處 也有需要修改的部分)  不知道未來網址會不會變
'download_url = "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/" & version_number & "/linux64/chromedriver-linux64.zip"
'download_url = "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/" & version_number & "/mac-x64/chromedriver-mac-x64.zip"
'download_url = "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/" & version_number & "/win32/chromedriver-win32.zip"
download_url = "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/" & version_number & "/win64/chromedriver-win64.zip"
    Call objHTTP.Open("GET", download_url, False)
    Call objHTTP.Send("")

        Set fileStream = CreateObject("ADODB.Stream")  '使用adodb.stream 接受檔案
        With fileStream
            .Open
            .Type = 1 'adTypeBinary
            .Write objHTTP.responseBody
            .Position = 0
            .SaveToFile foler & "chromedriver.zip", 2 '  下載檔案到selenium資料夾    ' 2  adSaveCreateOverWrite
            .Close
        End With
        
        '使用shell+cmd殺舊的檔案   kill的方式被微軟封了
        Set oApp = CreateObject("Shell.Application")
        cmdCommand = "cmd /c del " & foler & "chromedriver.exe"     '使用cmd刪除檔案 cmd /c del "path"
        Call Shell(cmdCommand, vbHide)                             '殺掉舊的chromedriver.exe
        cmdCommand = "cmd /c del " & foler & "LICENSE.chromedriver"
        Call Shell(cmdCommand, vbHide)                             '殺掉舊的LICENSE.chromedriver
            '若沒殺成功 該程序沒關閉 無法刪除時 將執行佇列中的檔案關閉  'cmd /c taskkill /F /IM chromedriver.exe 是殺佇列
            If Dir(foler & "chromedriver.exe") <> "" Then
            cmdCommand = "cmd /c taskkill /F /IM chromedriver.exe && cmd /c del " & foler & "chromedriver.exe"  '殺掉舊的chromedriver.exe
            Call Shell(cmdCommand, vbHide)
            End If
            If Dir(foler & "LICENSE.chromedriver") <> "" Then
            cmdCommand = "cmd /c taskkill /F /IM LICENSE.chromedriver && cmd /c del " & foler & "LICENSE.chromedriver"  '殺掉舊的LICENSE.chromedriver
            Call Shell(cmdCommand, vbHide)
            End If
            
        '將新的chromedriver.exe解壓縮到資料夾中  16是強制取代選項 注意壓縮檔內的檔名 會因系統位元不同資料夾不同
        oApp.Namespace(foler).CopyHere _
            oApp.Namespace(foler & "chromedriver.zip\chromedriver-win64").items, 16

End Sub

TOP

Driver.Get "https://tw.stock.yahoo.com/quote/1101.TW"

   Do: Set ID0 = Driver.findelementsbyID("main-0-QuoteHeader-Proxy")
        If ID0.Count > 0 Then Exit Do
        Loop
   Do: Set spans = ID0(1).findelementsbytag("span")
        If spans.Count > 0 Then Exit Do
        Loop
    For Each Z In spans
    If Z.Text Like "*開盤*更新*" Then
    時間 = Replace(Z.Text, "開盤 | ", "")
    時間 = "資料時間:" & Replace(時間, " 更新", "")
    Exit For
    End If
    Next

TOP

回復 56# Scott090

Driver.Get "https://tw.stock.yahoo.com/quote/1101.TW"

   Do: Set ID0 = Driver.findelementsbyID("main-0-QuoteHeader-Proxy")
        If ID0.Count > 0 Then Exit Do
        Loop
   Do: Set spans = ID0(1).findelementsbytag("span")
        If spans.Count > 0 Then Exit Do
        Loop
    For Each Z In spans
    If Z.Text Like "*盤*更新*" Then
    sp = Split(Z.Text, " ")
    時間 = "資料時間:" & sp(2) & " " & sp(3)
    Exit For
    End If
    Next

TOP

        靜思自在 : 【是否發揮了良能?】人間壽命因為短暫,才更顯得珍貴。難得來一趟人間,應問是否為人間發揮了自己的良能,而不要一味求長壽。
返回列表 上一主題