从网站提取值

我试图从一个网站提取价格到单个条形码的Excel。 到目前为止,我已经find了一小段代码,并试图将它们放在一起。 我得到的唯一成功是条码进入search框,然后点击。 然后网站显示的结果,但我不能从网站的结果到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中的价格:在Sheet 1中的Cell 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