gpt4 book ai didi

html - 从网站中提取值(value)

转载 作者:行者123 更新时间:2023-11-28 02:38:51 25 4
gpt4 key购买 nike

我正在尝试将网站上的价格提取到单个条形码的 Excel 中。到目前为止,我已经找到了一小段代码并试图将它们组合在一起。我唯一的成功是将条形码放入搜索框,然后单击。然后网站会显示结果,但我无法将该结果从网站获取到 Excel。

Sub GetPriceFromWeb1()

Dim IE As New InternetExplorer
Dim doc As HTMLDocument

Dim htmlInput As IHTMLElement
Dim HTMLButton As IHTMLElement

IE.Visible = True
IE.Navigate " http://www.web1.com"

Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop

Set doc = IE.Document
Set htmlInput = doc.getElementById("ContentPlaceHolderDefault_mainContent_tabbedMediaVal_9_txtBarcode")
htmlInput.Focus
htmlInput.Value = "045986013729"

Application.Wait Now + TimeValue("00:00:01")

Set HTMLButton = doc.getElementById("ContentPlaceHolderDefault_mainContent_tabbedMediaVal_9_getValSmall")
HTMLButton.Focus
HTMLButton.Click

'After clicking the button, the page refreshes and shows the barcode, title and price and shows a message (which disappears in a short time that item has been added)

Dim tag
Dim tags As Object
Set tags = doc.getElementsByClassName("col_Price")

For Each tag In tags
If tag.className = "col_Price" Then
MsgBox tag.innerText
Exit For
End If
Next tag

End Sub

该网站具有以下 HTML 代码,用于我有兴趣提取的值:

<div class="row rowDetails_Media">
<div class="col_Delete"><span class=""><a id="ContentPlaceHolderDefault_mainContent_BasketContents_14_rptBasket_btnDelete_0" class="delete" href="javascript:__doPostBack('ctl00$ctl00$ctl00$ContentPlaceHolderDefault$mainContent$BasketContents_14$rptBasket$ctl00$btnDelete','')"></a></span></div>
<div class="col_Title">Presumed Innocent </div>
<div class="col_Items">1 </div>
<div class="col_Code">0085391203421 </div>
<div class="col_Price">0.05 </div>
<div class="clearfix"></div>
</div>

我想要的值是:

  1. col_Title 中的标题:单元格 B2 工作表 1 中的假定无辜
  2. col_Price 中的价格:工作表 1 中单元格 C2 中的 0.05

非常感谢您在这方面的帮助。

最佳答案

试一试...

Sub GetPriceFromWeb1()

Dim IE As New InternetExplorer
Dim doc As HTMLDocument

Dim htmlInput As IHTMLElement
Dim HTMLButton As IHTMLElement

IE.Visible = True
IE.Navigate " http://www.web1.com"

Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop

Set doc = IE.Document
Set htmlInput = doc.getElementById("ContentPlaceHolderDefault_mainContent_tabbedMediaVal_9_txtBarcode")
htmlInput.Focus
htmlInput.Value = "045986013729"

Application.Wait Now + TimeValue("00:00:01")

Set HTMLButton = doc.getElementById("ContentPlaceHolderDefault_mainContent_tabbedMediaVal_9_getValSmall")
HTMLButton.Focus
HTMLButton.Click

Do While IE.ReadyState <> READYSTATE_COMPLETE
Loop

Set doc = IE.Document

'After clicking the button, the page refreshes and shows the barcode, title and price and shows a message (which disappears in a short time that item has been added)


Dim Tag As MSHTML.IHTMLElement
Dim Tags As MSHTML.IHTMLElementCollection
Dim n As Integer

Set Tags = doc.getElementsByTagName("div")

For Each Tag In Tags
If Tag.className = "col_Title" Then
Sheets("Sheet1").Range("B2").Value = Tag.innerText
n = n + 1
ElseIf Tag.className = "col_Price" Then
Sheets("Sheet1").Range("C2").Value = Tag.innerText
n = n + 1
End If
If n = 2 Then Exit For
Next Tag

End Sub

关于html - 从网站中提取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45538319/

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