gpt4 book ai didi

vba - 使用 Visual Basic 从 Internet Explorer 中提取类中的值

转载 作者:行者123 更新时间:2023-12-04 20:41:47 26 4
gpt4 key购买 nike

使用此代码,我想从网站中检索特定标签的值并将其放在我的电子表格中:

Sub get_tit()
Dim wb As Object
Dim doc As Object
Dim sURL As String
Dim lastrow As Long

lastrow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row

Set wb = CreateObject("internetExplorer.Application")
wb.navigate "https://www.tyre-shopper.co.uk/search/205-55-16/V/91"
Do While wb.Busy
Loop

Set doc = wb.document

Price = SelectNodes("#more-tyres > li:nth-child(4) > div > div.result-buy > form > span.tyre-price > div.tyre-price-cost.tyres-1 > strong")
Range("A5").Value = Price
End Sub

我尝试使用 CSS 路径将其选为节点,但不能。
我也尝试从类里面选择它,但它再次不起作用

这是来自网站的代码,我想从中检索值 57.50
<span class="tyre-price">
Fully Fitted Price
<div class="tyre-price-cost tyres-1">

<strong>&pound;57.50</strong>
</div><div class="tyre-price-cost tyres-2" style="display:none">

<strong>&pound;115.00</strong>
</div><div class="tyre-price-cost tyres-3" style="display:none">

<strong>&pound;172.50</strong>
</div><div class="tyre-price-cost tyres-4" style="display:none">

<strong>&pound;230.00</strong>
</div><div class="tyre-price-cost tyres-5" style="display:none">

<strong>&pound;287.50</strong>
</div>
</span>

最佳答案

根据 getElementsByClassName method 直接提取元素,我从来没有取得过很大的成功。但循环浏览 getElementsByTagName method 返回的集合并且比较类属性似乎也至少有效。

Sub get_tit()
Dim wb As Object
Dim doc As Object
Dim sURL As String
Dim lastrow As Long
Dim iDIV As Long, sPrice As Variant, sib As Long
Dim eSIB As IHTMLElement

lastrow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row

Set wb = CreateObject("internetExplorer.Application")
wb.Visible = True
wb.navigate "https://www.tyre-shopper.co.uk/search/205-55-16/V/91"
Do While wb.Busy And wb.readyState <> 4
DoEvents
Loop

Set doc = wb.document

With doc.body
sPrice = Array(0, 0, 0, 0)
For iDIV = 0 To .getElementsByTagName("div").Length - 1
With .getElementsByTagName("div")(iDIV)
Select Case .className
Case "tyre-price-cost tyres-1"
sPrice(0) = .innerText
Case "tyre-price-cost tyres-2"
sPrice(1) = .innerText
Case "tyre-price-cost tyres-3"
sPrice(2) = .innerText
Case "tyre-price-cost tyres-4"
sPrice(3) = .innerText
With Sheet1
.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = sPrice(0)
.Cells(Rows.Count, "A").End(xlUp).Offset(0, 1) = sPrice(1)
.Cells(Rows.Count, "A").End(xlUp).Offset(0, 2) = sPrice(2)
.Cells(Rows.Count, "A").End(xlUp).Offset(0, 3) = sPrice(3)
End With
sPrice = Array(0, 0, 0, 0)
Case Else
End Select
End With
Next iDIV
End With
End Sub

tyre scrape

fwiw,我相信 IXMLHTTPRequest 会更好地为您服务使用 InternetExplorer object 基于刮比一个.

关于vba - 使用 Visual Basic 从 Internet Explorer 中提取类中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31496199/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com