ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[¤À¨É] Âà´«ieª«¥ó»yªk¬°Selenium(chrome) ?

[¤À¨É] Âà´«ieª«¥ó»yªk¬°Selenium(chrome) ?

Sub IE345678()
    Set Ie = CreateObject("InternetExplorer.Application")
    Ie.Navigate "https://mis.twse.com.tw/stock/group.jsp?type=fixed&ex=tse&ind=TIDX#STATISTICS"
Do While Ie.Busy Or Ie.ReadyState <> 4: DoEvents: Loop
    Dim I As Integer, S As Integer, k As Integer, J As Integer
     Dim Element
    Set Element = Ie.document.getelementsbytagname("table")
    With Sheets("sheet1")
      .Range("a1:f17").ClearContents
        For S = 4 To 5               
            For I = 0 To Element(S).Rows.Length - 1
                k = k + 1
                For J = 0 To Element(S).Rows(I).Cells.Length - 1  
               .Cells(k, J + 1) = Element(S).Rows(I).Cells(J).innerText
                Next
            Next
        Next
    End With
    Set Element = Nothing
end sub

½Ð±Ð¦¹ieª«¥ó¦p¦óÂà´«¦¨Selenium(chrome)ªº»yªk?

¦^´_ 1# t8899


Sub test()
Set driver = CreateObject("Selenium.ChromeDriver")
driver.get "https://mis.twse.com.tw/stock/group.jsp?type=fixed&ex=tse&ind=TIDX#STATISTICS"  '¦pªG¥d¦b³o¸Ì ÂsÄý¾¹¨S¶}¦¨ ­nª`·Nchromedriverª©¥»¬O§_§ó·s
Set tb = driver.findelementsbytag("table")
Cells.ClearContents
For Each Z In tb
r = Cells(Rows.Count, 1).End(3).Row + 3
Z.astable.toexcel Cells(r, 1)
Next
End Sub

TOP

¥»©«³Ì«á¥Ñ t8899 ©ó 2022-9-23 20:17 ½s¿è

¦^´_ 2# singo1232001
ÁÂÁ«ü¾É¡A³Ì«á­±»Ý¤£»Ý­n tb.quit ?? (¹³ie.quit)
¦pªG¥u­ntable 3 , »yªk¬O?

TOP

¥»©«³Ì«á¥Ñ t8899 ©ó 2022-9-23 21:01 ½s¿è

¦^´_ 2# singo1232001
¤W­±ªº°ÝÃD¤w§ä¨ìµª®×

1.§Ú¦pªG¥u­n tb("6") ªº²Ä¤TÄæ²Ä¤T¦C³o­ÓÀx¦sªº­È(¹Ï¤¤¶À¦â)ªº»yªk¬O?
2.°õ¦æ®É ÁôÂÃchromeµøµ¡ªº»yªk ??

TOP

¦^´_ 4# t8899


    Set tb = driver.FindElementsByTag("table")(6)
MsgBox tb.FindElementsByTag("tr")(3).FindElementsByTag("td")(3).Text

TOP

¦^´_  t8899


    Set tb = driver.FindElementsByTag("table")(6)
MsgBox tb.FindElementsByTag("tr ...
quickfixer µoªí©ó 2022-9-23 23:53

ÁÂÁ«ü¾É
1 °õ¦æ®É ÁôÂÃchromeµøµ¡ªº»yªk ??
2.¥u§ìªí®æ6ªº²Ä¤GÄæ»yªk?

TOP

¥»©«³Ì«á¥Ñ singo1232001 ©ó 2022-9-24 08:54 ½s¿è

¦^´_ 6# t8899


    Sub test2()
Cells.ClearContents

Set driver = CreateObject("Selenium.ChromeDriver")
'driver.AddArgument ("headless") 'µLÀYµøµ¡
  'driver.Wait 1500 '¨S¦³¥[µ¥«Ý1.5¬í ·|¤Ö¤@¦¸ºô­¶§¹¦¨°±¹y ¸ê®Æ¨Ó¤£¤Îªñ¨Ó ¬í¼Æ¨Ì·Ó¹q¸£®Ä¯àºô¸ô³t«×½Õ¾ã
    driver.get "https://mis.twse.com.tw/stock/group.jsp?type=fixed&ex=tse&ind=TIDX#STATISTICS"

Set tbl6 = driver.findelementsbytag("table")(6)    '<table> ²Ä¤»­Ótable
                  r = Cells(Rows.Count, 1).End(3).Row + 1
                     Cells(r, 1) = tbl6.findelementsbytag("th")(2).Text '<th> "¾ãÅ饫³õªº¼ÐÃD"

  Set tby1 = tbl6.findelementsbytag("tbody")(1)      '<tbody> ²Ä¤@­Ótbody
    Set tr = tby1.findelementsbytag("tr")          '§ä¨ì¥þ³¡tr«á  ¨C­Ótrªº²Ä¤G­Ótd

       For Each Z In tr
           r = Cells(Rows.Count, 1).End(3).Row + 1
          Cells(r, 1) = Z.findelementsbytag("td")(2).Text   'td²Ä¤G
       Next
End Sub

¾á¤ß­ìpo¬Ý¤£À´¶¶§Ç (¥i¥H¹ï·ÓµÛºô­¶ªº¼ÐÅÒ¬Ý)
¥t¥~¤£¥u³oºØ¼ÐÅÒ¦¡ªº¬d§ä
¤]¦³¨ä¥LºØÃþªº¤èªk
¥u¬O³oºØ¤ñ¸û¤£®e©öÅý·s¤â¦bºô­¶¤¤°g¸ô
·í§A½m±o«Ü¯Â¼ôªº®É­Ô,¥i¥H¹³5¼Óq¤j¨º¼Ë³sµÛ¼g¤ñ¸û§Ö

TOP

¥»©«³Ì«á¥Ñ singo1232001 ©ó 2022-9-24 13:37 ½s¿è

Sub ¤À¸Ñ¨BÆJª©()
Cells.ClearContents
Set driver = CreateObject("Selenium.ChromeDriver")
    driver.AddArgument ("headless") 'µLÀYµøµ¡
    driver.Wait 1500
    driver.get "https://mis.twse.com.tw/stock/group.jsp?type=fixed&ex=tse&ind=TIDX#STATISTICS"

Set tbl6 = driver.findelementsbytag("table")(6)
  Set tby1 = tbl6.findelementsbytag("tbody")(1)
    Set tr2 = tby1.findelementsbytag("tr")(2)
      Set td2 = tr2.findelementsbytag("td")(3)
        Cells(1, 1) = td2.Text
End Sub


Sub ¤@®ð¨þ¦¨ª©()
Cells.ClearContents
Set driver = CreateObject("Selenium.ChromeDriver")
    driver.AddArgument ("headless") 'µLÀYµøµ¡
    driver.Wait 1500
    driver.get "https://mis.twse.com.tw/stock/group.jsp?type=fixed&ex=tse&ind=TIDX#STATISTICS"

Cells(1, 1) = driver.findelementsbytag("table")(6). _
               findelementsbytag("tbody")(1). _
               findelementsbytag("tr")(2). _
               findelementsbytag("td")(3). _
               Text
End Sub

'¨âºØ¼gªk³£¤@¼Ë







'¦pªG­n¥[¤W§¹¥þ¦Û°Ê§ó·s
Sub Chrome§ó·s®É¦Û°Ê§ó·s_and_¤À¸Ñ¨BÆJª©()

Do
    On Error GoTo 0
    On Error Resume Next
    Set driver = CreateObject("Selenium.ChromeDriver")
        driver.AddArgument ("headless")
    Err = 0
    driver.Start
    If Err <> 0 Then
      If Exit5 > 5 Then Exit Sub  '­Y°õ¦æ¶W¹L5¦¸´N¤£¶]¤F
      driver.Quit
      Set driver = Nothing
      Exit5 = Exit5 + 1
      Call updataSelenium
    End If
Loop Until Err = 0
Set driver = Nothing




Cells.ClearContents
Set driver = CreateObject("Selenium.ChromeDriver")
    driver.AddArgument ("headless") 'µLÀYµøµ¡
    driver.Wait 1500
    driver.get "https://mis.twse.com.tw/stock/group.jsp?type=fixed&ex=tse&ind=TIDX#STATISTICS"

Cells(1, 1) = driver.findelementsbytag("table")(6). _
               findelementsbytag("tbody")(1). _
               findelementsbytag("tr")(2). _
               findelementsbytag("td")(3). _
               Text
End Sub

Sub updataSelenium()  '¦pªG¤¤³~´N¸õÂ÷ ¥i¯à¬OÀɮצì¸m­n­«·s½T»{ ©ÎªÌ ª©¥»ÀɮרS§R°£¾É­Pµ{§ÇµLªk°õ¦æ ¤]¥i¯à¬OÀ£ÁYÀɸÑÀ£¹Lµ{¥X°ÝÃD ¤]¥i¯à¬O¤U¸üÀÉ®×¥X°ÝÃD ¥D­nF8³v¨B¬Ý­þ¬q¸õÂ÷
'§ä¨ìchromedriverªºÀɮצì¸m ¦³ªº¹q¸£¦ì¸m¤£¦P ³q±`¬O³o¨â­Ó¦ì¸m¨ä¤@
'½T»{chromedriver¥¿½TªºÀɮ׸ô®| »P¥¿½Tªº¸ê®Æ§¨¸ô®|
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, "\"))

'¬d§ä¹q¸£·í«eªºgoogleª©¥»¸¹
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)

'¬d§ä¹q¸£·í«eªºchromeDriverª©¥»¸¹
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

'­Y¤£²Å ,«h¤W©xºô¬d³Ì·schromeDriverª©¥»¸¹
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"
    '§Q¥Î³Ì·sªºchromeDriverª©¥»¸¹ §ä¨ì¤U¸ü¸ô®|
    Call objHttp.Open("GET", download_url, False)
    Call objHttp.Send("")
        
        '¤U¸üÀÉ®×,¨Ã§ó¥¿¦¨À£ÁYÀÉ°ÆÀɦW
        Set fileStream = CreateObject("ADODB.Stream")
        With fileStream
            .Open
            .Type = 1 'adTypeBinary
            .Write objHttp.responseBody
            .Position = 0
            .SaveToFile foler & "chromedriver.zip", 2
            .Close
        End With
        
        '¸ÑÀ£ÁY(¸ÑÀ£«eµ{§Ç·|¥ý§R±¼Âªº)
        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

TOP

¥»©«³Ì«á¥Ñ t8899 ©ó 2022-9-24 14:03 ½s¿è

¦^´_ 8# singo1232001
ÁÂÁ±z¼ö¤ß«ü¾É
§Ú¬Ýµ{§Ç°õ¦æ§¹CHROME ·|¦Û°ÊÃö³¬(°h¥X)¡A¤£¥Î¦A¦h  DRIVER.Quit ??

TOP

¥»©«³Ì«á¥Ñ singo1232001 ©ó 2022-9-24 14:51 ½s¿è

¦^´_ 9# t8899


¹ï
­Y§A¤£·QÃö³¬ ¥i¥H³o¼Ë¼g

Dim driver '<--³o­n¸m³» ³Ì³»³¡ ¦b³o¤§¤W¤£¯à¦³¥ô¦ó¦r
Sub ¤£Ãö³¬¤À¸Ñ¨BÆJª©()
Cells.ClearContents
Set driver = CreateObject("Selenium.ChromeDriver")
    'driver.AddArgument ("headless") 'µLÀYµøµ¡
    driver.Wait 1500
    driver.get "https://mis.twse.com.tw/stock/group.jsp?type=fixed&ex=tse&ind=TIDX#STATISTICS"

Set tbl6 = driver.findelementsbytag("table")(6)
  Set tby1 = tbl6.findelementsbytag("tbody")(1)
    Set tr2 = tby1.findelementsbytag("tr")(2)
      Set td2 = tr2.findelementsbytag("td")(3)
        Cells(1, 1) = td2.Text
End Sub

Sub Ãö³¬¤lµ{§Ç()
On Error Resume Next
If IsObject(driver) Then
driver.Quit
Set driver = Nothing
End If
End Sub

§Ñ¤F»¡©ú
¤À¸Ñ¨BÆJª©¦³­Ó¦n³B
´N¬O¥i¥H¦h°µ if isobject  + do loop+wait §PÂ_
¥t¥~¤£¥u¥i¥H§PÂ_¤¸¯À¦s¦b ¤]¥i¥H§PÂ_­È¬O§_¬°ªÅ ©ÎªÌ§PÂ_ª«¥ó¼Æ¶q¬O§_>1
ÁקK¦³¨Çºô­¶·|ºCºC¤~¥X²{¸ê®Æ(³oÃþºô­¶«Ü±`¨£ ¨Ò¦pyahoo)

¤£¹LÁÙ¦³¥t¥~¨âºØÃþ«¬¤]­n¤p¤ß
¤@ºØ¬O ¶W¥X¥iµø½d³ò¥~ ·|§ä¤£¨ì¤¸¯À(³oºØºô­¶¤£¦h)
¤@ºØ¬O ­n¤U©Ôºô­¶ ¤~·|¸õ¥X·s¤¸¯À(¦pFB)
³oÃþºô­¶´N­n¦hªáÂI¥\¤Ò
³q±`°ê¥~½×¾Â³£¦³±Ð §V¤O¬d ¬dªº¨ì

TOP

        ÀR«ä¦Û¦b : ¡i¦æµ½­n¤Î®É¡j¦æµ½­n¤Î®É¡A¥\¼w­n«ùÄò¡C¦p¿N¶}¤ô¤@¯ë¡A¥¼¿N¶}¤§«e¤d¸U¤£­n°±º¶¤õ­Ô¡A§_«h­«¨Ó´N¤Ó¶O¨Æ¤F¡C
ªð¦^¦Cªí ¤W¤@¥DÃD