【问题标题】:Extracting Values from Websites从网站中提取价值
【发布时间】:2018-01-14 06:03:55
【问题描述】:

我正在尝试将网站中的价格提取到单个条形码的 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 中的标题:Presumed Innocent 在单元格 B2 表 1 中
  2. col_Price 中的价格:0.05 在工作表 1 的单元格 C2 中。

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

【问题讨论】:

  • 点击按钮后,再次尝试等待,直到就绪状态完成。
  • 我也试过了,问题是关于提取标题和价格。谢谢
  • Set doc = IE.Document - 每次重新加载页面时,您都需要再次执行此操作:您正在搜索上一页(并且不再存在...)
  • @Tim Williams 是的,这就是问题所在,代码搜索了上一页,即使使用 Set doc = IE.Document 后它仍然会这样做

标签: html vba excel


【解决方案1】:

试试这个...

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

【讨论】:

  • 感谢您的代码。如果已经有已扫描的条码添加到购物车,您的代码会为我提供标题和价格。因此,每当我们输入新条形码时,表格都会更新并添加新行。我需要最新的标题和价格,通常在表格的开头。感谢您的帮助
  • @Tim Williams 嗨,退出的位置。谢谢
  • @StarShines 我想我回答了你原来的问题,即不工作的部分。这应该让您了解代码中使用的算法。如果您有任何进一步的疑问,请接受答案并打开一个新问题来结束此问题。
  • 感谢您的代码和帮助。我发现每次使用条形码添加项目时表格都会更新,这就是问题所在。
  • 这是因为值总是写在 B2 和 C2 中。声明一个长变量(比如“LR”)来保存 B 列中可用的下一个空行,然后在代码中使用 Range("B" & LR).value = 和 Range("C" & LR).value =。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-26
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
  • 2020-05-28
  • 2014-09-13
相关资源
最近更新 更多