2015-07-18 88 views
2

有了這個代碼,我想檢索值從網站的特定標籤,並把它放在我的電子表格中從Internet Explorer類的值:提取使用Visual Basic

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> 

回答

1

我從來沒有過根據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,我相信你會與使用InternetExplorer objectIXMLHTTPRequest基於困境比一個得到更好的服務。