Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel InnerText对于特定的跨度类返回空_Excel_Vba_Web Scraping - Fatal编程技术网

Excel InnerText对于特定的跨度类返回空

Excel InnerText对于特定的跨度类返回空,excel,vba,web-scraping,Excel,Vba,Web Scraping,我试图从网站上检索常规(126,37欧元)和降价(101,10欧元)的价格信息 简化的HTML代码如下所示: <div class="vw-productFeatures "> <ul class="feature-list -price-container"> <li class="feature -price"> <span class="value">126,37</span> </li>

我试图从网站上检索常规(126,37欧元)和降价(101,10欧元)的价格信息

简化的HTML代码如下所示:

<div class="vw-productFeatures ">
  <ul class="feature-list -price-container">
    <li class="feature -price">
      <span class="value">126,37</span>
    </li>
  </ul>
  <ul class="feature-list vw-productVoucher">
    <li class="voucher-information">Mit Code
      <span class="voucher-reduced-price">101,10</span>
    </li>
  </ul>
</div>
我得到的输出:

Regular price: 126,37
Reduced price: 
调试器显示它能够找到正确的span类,但它没有任何包含价格信息的属性(包括innerText)


如何获取降价信息?

没有降价的
-hide
类:

ulClass = "feature-list vw-productVoucher"
您可以使用简单的选择器,通过
querySelector
()获取这两种价格,而不是使用具有不必要迭代的复杂方法

regularPrice = HTMLDoc.querySelector(".-price .value").innerText
reducedPrice = HTMLDoc.querySelector(".voucher-reduced-price").innerText
更新:
Vaucher在这里,根据
产品车间id
和日期进行计算。

有时,当页面的大部分内容依赖于API调用时,使用浏览器自动化更容易

从性能的角度来看,它并不理想,但运行起来更快,而且在紧要关头也能正常工作。另一种方法是监视您与服务器之间的web流量,并查看是否可以模拟web请求以获得更低的价格。这会更快,但可能需要一点时间来弄清楚它是如何工作的

对于每种方法都要考虑权衡。下面是一些InternetExplorer自动化代码,用于检索我相信您正在查找的数据

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub GetReducedPrice()
    Dim text As String

    With CreateObject("internetexplorer.application")
        .navigate "https://www.rakuten.de/produkt/msi-b450-tomahawk-max-atx-mainboard-4x-ddr4-max-64gb-1x-dvi-d-1x-hdmi-14-1x-usb-c-31-2843843890"
         Do While .Busy And .readyState <> 4: DoEvents: Loop
         Sleep 1000 ' wait a little bit too
         text = .document.querySelector(".voucher-reduced-price").innerText
        .Quit
    End With

    Debug.Print "the reduced price is: " & text
End Sub
声明子睡眠库“kernel32”(ByVal-dwms长度)
子GetReducedPrice()
将文本变暗为字符串
使用CreateObject(“internetexplorer.application”)
.导航“https://www.rakuten.de/produkt/msi-b450-tomahawk-max-atx-mainboard-4x-ddr4-max-64gb-1x-dvi-d-1x-hdmi-14-1x-usb-c-31-2843843890"
执行While.Busy和.readyState 4:DoEvents:Loop
睡1000’再等等
text=.document.querySelector(“凭证降价”).innerText
退出
以
Debug.Print“降价为:”&文本
端接头
结果是:


降价为:101,10

我不是这方面的专家,但我认为没有理由在
ulClass
变量中包含
-hide
。这似乎会导致它与任何东西都不匹配。实际上,当我“检查”时,
ul类
似乎是
功能列表vw ProductCredence
。然而,当时没有对手。当我查看页面源代码并搜索
功能列表vw ProductCredential
,我可以看到它是
功能列表vw ProductCredential-hide
。感谢您的回答。你运行这个代码了吗?我仍然收到一个空的关于降价的回复。它是空的,来自另一个地方的降价。可能来自JavaScript()
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub GetReducedPrice()
    Dim text As String

    With CreateObject("internetexplorer.application")
        .navigate "https://www.rakuten.de/produkt/msi-b450-tomahawk-max-atx-mainboard-4x-ddr4-max-64gb-1x-dvi-d-1x-hdmi-14-1x-usb-c-31-2843843890"
         Do While .Busy And .readyState <> 4: DoEvents: Loop
         Sleep 1000 ' wait a little bit too
         text = .document.querySelector(".voucher-reduced-price").innerText
        .Quit
    End With

    Debug.Print "the reduced price is: " & text
End Sub