使用VBA进行Webscraping excel

时间:2017-06-03 16:22:26

标签: excel-vba web-scraping vba excel

我应该使用哪些代码从此HTML代码中提取价格?我想通过

获取元素
  • Id
  • tagname
  • 类名

网站网址: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
  

[占位符解释我的代码目前出现了什么问题]

1 个答案:

答案 0 :(得分: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时会发生什么,另一种解决方案可能会更清晰