Selenium
[attach]36496[/attach]請教我要抓圖中的綠色部份(含倒三角形),怎麼抓(語法)?
[url]https://tw.stock.yahoo.com/quote/2330[/url]
從網頁得知FindElementByXPath
//*[@id="qsp-overview-realtime-info"]/div[2]/div[2]/div/ul/li[8]/span[2]
這是倒三角形含數字
//*[@id="qsp-overview-realtime-info"]/div[2]/div[2]/div/ul/li[8]/span[2]/span
這只有倒三角形 [i=s] 本帖最後由 singo1232001 於 2023-7-20 03:01 編輯 [/i]
[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=121363&ptid=24009]1#[/url] [i]t8899[/i] [/b]
<div class="Jc(fe) Mend(4px) Fw(600) D(f) Ai(c) C($c-trend-up)" ' 這是紅色向上
<div class="Jc(fe) Mend(4px) Fw(600) D(f) Ai(c) C($c-trend-down)" '這是綠色 向下
所以
if 網頁元素物件.Attribute("class") Like "*C($c-trend-up)*" Then
debug.print "▲" '正數
else
debug.print "▼" '負數
end if
你也可以試著一個一個
debug.print 網頁元素物件.Attribute("class")
觀察是不是你要的 [i=s] 本帖最後由 singo1232001 於 2023-7-20 11:05 編輯 [/i]
[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=121363&ptid=24009]1#[/url] [i]t8899[/i] [/b]
Sub ttt()
Set driver = CreateObject("Selenium.ChromeDriver")
driver.get "https://tw.stock.yahoo.com/quote/2330"
driver.Wait 1000
Set ID_0 = driver.findelementbyID("qsp-overview-realtime-info")
Set ID_0_ul_S = ID_0.findelementsbytag("ul")
Set ID_0_ul_S_1_li_S = ID_0_ul_S(1).findelementsbytag("li")
For i = 1 To ID_0_ul_S_1_li_S.Count
Set x = ID_0_ul_S_1_li_S(i).findelementsbytag("span")
If x(1).Text Like "*漲跌*" Then
Select Case True
Case x(2).Attribute("class") Like "*C($c-trend-up)*": tx = "▲ " & x(2).Text
Case x(2).Attribute("class") Like "*C($c-trend-down)*": tx = "▼ " & x(2).Text
Case Else: tx = x(2).Text
End Select
Debug.Print x(1).Text & " " & tx
Else
Debug.Print x(1).Text & " " & x(2).Text
End If
Next
Stop
End Sub [i=s] 本帖最後由 singo1232001 於 2023-7-20 11:57 編輯 [/i]
[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=121363&ptid=24009]1#[/url] [i]t8899[/i] [/b]
如果你用xpath 就是這樣
Sub tttt()
Set driver = CreateObject("Selenium.ChromeDriver")
driver.get "https://tw.stock.yahoo.com/quote/2330"
driver.Wait 1000
Set xp = driver.findelementbyxpath("//*[@id=""qsp-overview-realtime-info""]/div[2]/div[2]/div/ul/li[8]")
Set sn = xp.findelement[color=Red]s[/color]bytag("span")
Select Case True
Case sn(2).Attribute("class") Like "*C($c-trend-up)*": tx = "▲ "
Case sn(2).Attribute("class") Like "*C($c-trend-down)*": tx = "▼ "
Case Else: tx = " - "
End Select
Debug.Print sn(1).Text & " " & tx & " " & sn(2).Text
Stop
End Sub [b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=121664&ptid=24009]4#[/url] [i]singo1232001[/i] [/b]
謝謝指導 [b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=121664&ptid=24009]4#[/url] [i]singo1232001[/i] [/b]
drIVER.get "https://finance.yahoo.com/quote/TSM/"
請教如果連線超過3秒有問題?則跳出的語法? [i=s] 本帖最後由 singo1232001 於 2023-8-24 18:13 編輯 [/i]
[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=121762&ptid=24009]6#[/url] [i]t8899[/i] [/b]
Sub 時間內沒加載完就算失敗()
t = Timer '測試用可刪
Set driver = CreateObject("selenium.chromedriver")
driver.Timeouts.pageload = 10000 '要是秒數過低 沒加載完 就會強制關閉 可以試著測試3秒以下
On Error Resume Next
driver.get "https://finance.yahoo.com/quote/TSM/"
If Err.Number <> 0 Then
Err.Clear
driver.Quit
End If
On Error GoTo 0
Debug.Print Timer - t '測試用可刪
End Sub
因為不曉得你的需求
所以我順便把重連三次的寫法也給你
Sub 重連三次每次等三秒_第四次就失敗()
Set driver = CreateObject("selenium.chromedriver")
On Error Resume Next
Do
driver.Get "https://finance.yahoo.com111111111/quote/TSM/"
If Err.Number <> 0 Then
Err.Clear
Application.Wait Now + TimeValue("00:00:03")
rT = rT + 1
If rT = 3 Then driver.Quit: Exit Do
Else
Exit Do
End If
Loop
On Error GoTo 0
End Sub [b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=121763&ptid=24009]7#[/url] [i]singo1232001[/i] [/b]
謝謝指導 [b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=121764&ptid=24009]8#[/url] [i]t8899[/i] [/b]
請問如何在此網頁 "https://finance.yahoo.com/quote/TSM/"
取得下列這一些資料 ?
謝謝
Previous Close 549.00
Open 551.00
Bid 552.00 x 0
Ask 553.00 x 0
Day's Range 546.00 - 553.00
52 Week Range 370.00 - 594.00
Volume 10,650,708
Avg. Volume 22,277,577 [b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=121785&ptid=24009]9#[/url] [i]Scott090[/i] [/b]
Sub test()
Set driver = CreateObject("selenium.chromedriver")
driver.get "https://finance.yahoo.com/quote/TSM/"
Do: Set ids = driver.findelementsbyId("quote-summary")
If ids.Count > 0 Then Exit Do
Loop
Do: Set tby = ids(1).findelementsbytag("tbody")
If tby.Count > 0 Then Exit Do
Loop
Debug.Print tby(1).Text
Set trs = tby(1).findelementsbytag("tr")
ReDim ar(1 To trs.Count, 1)
For i = 1 To trs.Count
Set tds = trs(i).findelementsbytag("td")
ar(i, 0) = tds(1).Text
ar(i, 1) = tds(2).Text
Next
Cells.ClearContents
Range("a1").Resize(UBound(ar), 2) = ar
End Sub [b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=121786&ptid=24009]10#[/url] [i]singo1232001[/i] [/b]
感恩詳解。
請指導一下在瀏覽器 用"開發者工具" :
在這個例子中為什麼知道要找
Id("quote-summary")
tag("tbody")
tag("tr")
tag("td")
? [i=s] 本帖最後由 singo1232001 於 2023-8-30 09:09 編輯 [/i]
[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=121788&ptid=24009]11#[/url] [i]Scott090[/i] [/b]
把找元素想像成,跟檔案總管找資料夾一樣
常用的有父子關係 兄弟關係 或者ID唯一性
而父元素 有的用ID 有的用tag 有的是順位 有的用副加屬性attribute
這4種方式可以協助你找到需要的父元素 或 子元素
當下看看那些方式適合使用
通常用於網頁有異步的情況 才會需要順著關係或逆著關係找
若網頁沒異步 就不用關係找 直接用byXpath完整路徑找到就可以
就如同 直接複製資料夾路徑那樣 就能前往資料夾位置
通常能解決80~90%尋找問題
補充:
還可能遇到ifram有框架網站
還可能遇到需要用execute注入css 解決剩下10~20%其他有對元素動手腳,網站性特殊保護的問題 [b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=121789&ptid=24009]12#[/url] [i]singo1232001[/i] [/b]
謝謝指導, 我試試看能找出甚麼。
頁:
[1]