我正在嘗試獲取標題和價格,并使用 vba selenium 從無序的 html 串列中將其列印到 excel 表中。
我嘗試了各種方法,大多數教程都有一個帶有 id 標簽的漂亮干凈的 html 示例。
這是我擁有的 vba 代碼。它不會回傳錯誤,但不會引入任何內容。
Sub test2()
Dim driver As New WebDriver
Dim rowc, cc, columnC As Integer
rowc = 2
Application.ScreenUpdating = False
driver.Start "chrome"
driver.Get "https://www.fence-depot.com/fence/36-inch-auburn-residential-aluminum-fence/"
For Each li In driver.FindElementByClass("wys-right").FindElementByTag("h4").FindElementsByTag("ul")
cc = 1
For Each t In li.FindElementsByTag("li")
Sheet1.Cells(1, cc).Value = t.Text
cc = cc 1
Next t
Next li
Application.Wait Now TimeValue("00:00:20")
End Sub
這是我試圖訪問的 html 代碼
<div class="wys-right" style="float: left; width: 350px; padding-bottom: 10px; min-height: 520px;">
<h4>Pricing:</h4>
<ul>
<li><span style="color: #808080;">6' Wide Panel =</span> <strong><span style="color: #000000;">$65.01 (2 Rail) </span></strong></li>
<li><span style="color: #888888;">6' Wide Panel = <strong><span style="color: #000000;">$74.91 (3 Rail)</span></strong></span></li>
<li><span style="color: #808080;">2" x 2" x .062" Lines, Ends, and Corner <span style="text-decoration: underline; color: #ff6600;"><span style="text-decoration: underline;"><a title="Punched Posts for an Aluminum Fence" href="https://fence-depot.com/punched-posts-for-an-aluminum-fence/?blog=cat" target="_blank" rel="noopener noreferrer"><span style="color: #ff6600; text-decoration: underline;">Posts</span></a> </span></span>= </span><span style="color: #000000;"><strong>$21.05</strong></span></li>
<li><span style="color: #808080;">2.5" x 2.5" Upgraded Lines, Ends, Corner Posts =</span> <strong><span style="color: #000000;">$34.05</span></strong></li>
<li><span style="color: #808080;">2" x 2" x .125" Gate Posts =</span> <strong><span style="color: #000000;">$36.53</span></strong></li>
<li><span style="color: #000000;"><span style="color: #808080;">3" x 3" x .125" Upgraded Gate Posts =</span></span><strong><span style="color: #000000;"> $65.62</span></strong></li>
<li><span style="color: #000000;"><span style="color: #808080;">4" x 4" x .125" Upgraded Gate Posts = </span></span><strong><span style="color: #000000;">$76.15</span></strong></li>
<li><span style="color: #000000;"><span style="color: #808080;">6" x 6" x .125" Upgraded Gate Posts =</span></span><strong><span style="color: #000000;"> $122.58 </span></strong></li>
</ul>
uj5u.com熱心網友回復:
我做了一些可能是你想要的改變。如果不是,您應該能夠破譯它。
主要是你的問題是 UL 不是 H4 的孩子,所以它永遠不會找到它。另外,我不知道物件模型將 UL 元素標識為 LI 元素的集合。我認為您必須明確找到 LI 標簽。可能是錯的。
Sub test2()
Dim driver As New webdriver
Dim rowc, cc, columnC As Integer
rowc = 2
Application.ScreenUpdating = False
driver.Start "chrome"
driver.Get "https://www.fence-depot.com/fence/36-inch-auburn-residential-aluminum-fence/"
r = 0
For Each ul In driver.FindElementByClass("wys-right").FindElementsByTag("ul")
r = r 1: cc = 0
For Each li In ul.FindElementsByTag("li")
cc = cc 1
Sheet1.Cells(r, cc).Value = li.Text
Next li
Next ul
Application.ScreenUpdating = True
' Application.Wait Now TimeValue("00:00:20")
End Sub
uj5u.com熱心網友回復:
您可以使用 xmlhttp 而不是瀏覽器的開銷。您需要傳入一個可識別的用戶代理標頭。
然后,您可以li通過將第一個ul元素中的元素與具有 class 的父元素一起收集來定位適當的元素wys-right。如果您拆分li文本," = "那么您可以將價格和描述寫出到單獨的列中。
您的里程可能因 Office 版本而異。這是使用 2019 撰寫的。
您可以使用 Selenium 類似的邏輯,但將 css 選擇器串列傳遞.wys-right ul:nth-of-type(1) li給FindElementsByCss()然后For Each回傳的WebElements集合并拆分.text每個WebElement.
Option Explicit
Public Sub WriteOutPrices()
'tools > references > Microsoft HTML Object Library
Dim html As MSHTML.HTMLDocument, xhr As Object
Set xhr = CreateObject("MSXML2.XMLHTTP")
Set html = New MSHTML.HTMLDocument
With xhr
.Open "GET", "https://www.fence-depot.com/fence/36-inch-auburn-residential-aluminum-fence/", False
.setRequestHeader "User-Agent", "Safari/537.36"
.send
html.body.innerHTML = .responseText
Debug.Print html.Title
End With
Dim ws As Worksheet, i As Long
Set ws = ThisWorkbook.Worksheets(1)
ws.Cells(1, 1).value = html.querySelector("h1").innerText
With html.querySelectorAll(".wys-right ul:nth-of-type(1) li")
For i = 0 To .length - 1
Dim arr() As String
arr = Split(.Item(i).innerText, " = ")
ws.Cells(i 3, 1) = arr(0)
ws.Cells(i 3, 2) = arr(1)
Next
End With
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/335740.html
