0

Which code should I use to extract price from this HTML code? I would like to get element either by

  • Id
  • tagname
  • classname

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

Let's say I want to fetch data for "price" in below HTML (i.e. get this "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>

Here is my code:

    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

[ Placeholder to explain what is currently going wrong with my code ]

Community
  • 1
  • 1
  • 2
    You should be posting your code/efforts and we should be dealing with your errors/mistakes. Not many is going to write a code for you to fetch the price. – Tehscript Jun 03 '17 at 16:59
  • Sorry that I did not post the code. Please check now, I have updated the question – Niyaz Ahmed Siddiqui Jun 03 '17 at 17:02
  • `objIE.Document.getElementsByClassName("sale")(1)` is one way, `objIE.Document.getElementById("spPriceId")` is another. You should inspect the source code and you will see the rest is easy. – Tehscript Jun 03 '17 at 18:29

1 Answers1

0

There is something happening with javascript that I cannot understand, and might change the way you can scrap your thing.

Here, it is possible to scrape the price with outertext once you have 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

Another solution might be cleaner if you can see what happens if you deactivate javascript

J. Chomel
  • 8,193
  • 15
  • 41
  • 69