2017-06-03 68 views
0

我應該使用哪些代碼來從此HTML代碼中提取價格?我想通過使用VBA的網頁掃描excel

  • 編號
  • 標記名
  • 類名要麼得到元素

網址:https://www.tatacliq.com/vivo-v5-32gb-gold-4-gb-ram-dual-sim-4g/p-mp000000000734559

比方說,我要爲「價格」獲取數據在下面HTML(即得到這個「14999」):

<h3 class="company author"> 
    </h3> 
    <div itemprop="offers" itemscope="" itemtype="http://schema.org/Offer" 
    class="price"> 
    <p class="old" id="mrpPriceId" style="display:none"> 
    </p> 
    <p class="sale" id="mopPriceId" style="display:none"> 
    </p> 
    <p class="sale" id="spPriceId" style="display:none"> 
    <!-- For TPR-4358 Start --> 
    <span itemprop="price">14999.00</span> 
    <span itemprop="priceCurrency">INR</span> 
    <meta itemprop="itemCondition" content="http://schema.org/NewCondition" 
    /> 
    <meta itemprop="availability" 
    content="http://schema.org/InStock"/>Available online</meta> 
    <!-- For TPR-4358 End --> 
    </p> 
    <p class="savings pdp-savings" id="savingsOnProductId" 
    style="display:none">               
     <span></span> 
    </p>  
    <br> 

這裏是我的代碼:

Function Scraptatacliq(tatacliq_url As String, the_display_price As String, the_display_seller As String) 

    ''MsgBox "Inside the function" 

    'Dim objIE As New InternetExplorerMedium 
    Scraptatacliq = "" 
    If tatacliq_url = "" Then Exit Function 


    the_display_price = "" 
    the_display_seller = "" 

    'MsgBox tatacliq_url 
    the_start: 

    Dim objIE As Object 
    Set objIE = CreateObject("InternetExplorer.Application") 
    'Set objIE = New InternetExplorerMedium 
    objIE.Top = 0 
    objIE.Left = 0 
    objIE.Height = 800 
    'objIE.Visible = True 
    objIE.Navigate (tatacliq_url) 

    'MsgBox tatacliq_url 

    Do 
    DoEvents 
    If Err.Number <> 0 Then 
    objIE.Quit 
    Set objIE = Nothing 
    GoTo the_start: 
    End If 
    Loop Until objIE.ReadyState = 4 

'Loop 
    Count = 500000 
    Do 
    If Count <> 0 Then 
    Count = Count - 1 
    End If 
    Loop Until Count = 0 

'EXTRACT DISPLAY PRICE 
'On Error Resume Next 
'MsgBox "I am about to extract display price" 
    Set the_input_element =  objIE.Document.getElementById("spPriceId").getElementsbyTagName("price") 
    product_display_price = the_input_element.innertext 
'MsgBox product_display_price 
'If product_display_price = "" Then 
    ' Set the_input_element = 
    objIE.Document.getElementById("product_list_price") 
'  product_display_price = the_input_element.innertext 
    'End If 

[預留解釋什麼是目前我的代碼腳麻]

+2

你應該張貼會發生什麼你代碼/努力,我們應該處理你的錯誤/錯誤。沒有多少人會爲你寫代碼來獲取價格。 – Tehscript

+0

對不起,我沒有發佈代碼。請檢查現在,我已更新問題 –

+0

'objIE.Document.getElementsByClassName(「sale」)(1)'是一種方法,'objIE.Document.getElementById(「spPriceId」)'是另一種方法。你應該檢查源代碼,你會看到其餘的很容易。 – Tehscript

回答

0

也有一些是用JavaScript,我無法理解發生的,有能改變你可以放棄你的東西的方式。

這裏,可以颳去與outertext的價格,一旦你有objIE.Document.getElementById("spPriceId")

Function Scraptatacliq(tatacliq_url As String) 
    Scraptatacliq = "" 
    If tatacliq_url = "" Then Exit Function 
    the_display_price = "" 
    the_display_seller = "" 

the_start: 
    Dim objIE As Object 
    Set objIE = CreateObject("InternetExplorer.Application") 
    objIE.Top = 0 
    objIE.Left = 0 
    objIE.Height = 800 
    objIE.Navigate (tatacliq_url) 

    Do 
    DoEvents 
    If Err.Number <> 0 Then 
     objIE.Quit 
     Set objIE = Nothing 
     GoTo the_start: 
    End If 
    Loop Until objIE.ReadyState = 4 

    objIE.Visible = True 
    Set the_input_element = objIE.Document.getElementById("spPriceId") 
    product_display_price = the_input_element.outertext 
    MsgBox product_display_price 

End Function 

另一種解決方案可能是清潔劑,如果你可以看到,如果你禁用JavaScript的