从网站中提取价值

时间:2017-08-07 01:42:05

标签: html excel vba excel-vba

我正在尝试将网站的价格提取到单个条形码的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中的标题:推定无辜在Cell B2 Sheet 1中
  2. 第1页中单元格C2中col_Price中的价格: 0.05
  3. 我非常感谢你在这方面的帮助。

1 个答案:

答案 0 :(得分:0)

试一试......

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