返回列表 上一主題 發帖

[分享] 轉換ie物件語法為Selenium(chrome) ?

[分享] 轉換ie物件語法為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物件如何轉換成Selenium(chrome)的語法?

本帖最後由 singo1232001 於 2022-10-19 11:55 編輯

回復 32# singo1232001


另外給一些沒安裝過selenium的連結
http://forum.twbts.com/thread-23709-1-3.html

爬蟲抓上漲跌下三角形.zip (15.57 KB)

TOP

回復 31# singo1232001


更正一下(單列 跟 雙列)

Sub test()
Set DRIVER = CreateObject("Selenium.ChromeDriver")
DRIVER.Get "https://tw.stock.yahoo.com/quote/1101.TW"
Cells.ClearContents
Set ID0 = DRIVER.FindElementByID("qsp-overview-realtime-info")
Set UL1 = ID0.FindElementsBytag("ul")(1)
'單列
sp = Split(UL1.Text, Chr(10))
Cells(1, 1).Resize(UBound(sp) + 1, 1) = Application.Transpose(sp)

'雙列
u2 = UBound(sp) / 2
ReDim ar(1 To u2, 1 To 2)
For i = 0 To UBound(sp) Step 2
w = w + 1
ar(w, 1) = sp(i)
ar(w, 2) = sp(i + 1)
Next
Cells(1, 3).Resize(UBound(ar), 2) = ar
End Sub

TOP

本帖最後由 singo1232001 於 2022-10-19 10:54 編輯

回復 30# t8899

Sub test()
Set DRIVER = CreateObject("Selenium.ChromeDriver")
DRIVER.Get "https://tw.stock.yahoo.com/quote/1101.TW"
Cells.ClearContents
Set ID0 = DRIVER.FindElementByID("qsp-overview-realtime-info")
Set UL1 = ID0.FindElementsBytag("ul")(1)

sp = Split(UL1.Text, Chr(10))
Cells(1, 1).Resize(UBound(sp), 1) = Application.Transpose(sp)
End Sub

補充 上下漲跌的三角形 跟負號 上述並沒有做特別處理
若有需要就必須利用tag單獨找出來,依照上三角形 與 下三角形 個別比對修改正負數

TOP

本帖最後由 t8899 於 2022-10-18 20:36 編輯

回復 28# singo1232001

Sub test()
Set DRIVER = CreateObject("Selenium.ChromeDriver")
DRIVER.Get "https://tw.stock.yahoo.com/quote/1101.TW"
Set td = DRIVER.FindElementByXPath("//*[@id=""qsp-overview-realtime-info""]/div[2]/div[2]/div/ul")
Cells.ClearContents
' td.AsTable.ToExcel Cells(1, 1)
End Sub
圖中框起來的灰色框框不是一個table ??
要如何以table方式匯入excel ?

TOP

回復  t8899


建議你把程式碼貼出來好了

可能是沒有CLOSE掉八

或者需求說一下
singo1232001 發表於 2022-10-16 07:42


解決了
我用public w1 , 又用 DIM w1 , 把dim w1 拿掉就好了
謝謝!

TOP

回復 27# t8899


建議你把程式碼貼出來好了

可能是沒有CLOSE掉八

或者需求說一下

TOP

回復  t8899

最上面除了
publice driver as object
多加一行
public w1,w2


w1 跟w2 沒跟過去
...
singo1232001 發表於 2022-10-16 02:16


試的結果 只能用 DRIVER.SwitchToPreviousWindow    ?
DRIVER.SwitchToWindowByTitle w1 '第一個交易所
DRIVER.SwitchToWindowByTitle w2 '第二個期交所
DRIVER.SwitchToNextWindow
這3個不行?

TOP

本帖最後由 singo1232001 於 2022-10-16 06:29 編輯

補充說明一下

用public 是全部的模組module 包括表單userform 都會吃到喔
   
如果其他模組也有用到 就用public

如果不想給其他模組Module 表單userform用的話

改用
dim driver as object
dim w1,w2

用dim 的話 同模組內的所有sub都能用 其他模組內的用不了

因為怕在其他模組的sub也有用到w1,w2 到時候衝突

TOP

回復 24# t8899

最上面除了
publice driver as object
多加一行
public w1,w2


w1 跟w2 沒跟過去
目前看來是 ""

TOP

        靜思自在 : 人生最大的成就是從失敗中站起來。
返回列表 上一主題