【问题标题】:VBA scraping get inner text from classVBA抓取从类中获取内部文本
【发布时间】:2022-07-22 07:06:56
【问题描述】:

我有一个从这里捕获数据的代码:

https://www.cigarsofcuba.co.uk/shop/cuban-cigars/bolivar-cigars/bolivar-belgravia-uk-regional-2015-cigars-box-of-10/

下面的代码运行良好,可以在 Excel 中捕获必要的数据。

Dim posts As Object, post As Object, r&

With CreateObject("InternetExplorer.Application")
    .Visible = False
    .navigate "https://www.cigarsofcuba.co.uk/shop/cuban-cigars/bolivar-cigars/bolivar-belgravia-uk-regional-2015-cigars-box-of-10/"
    While .Busy = True Or .readyState < 4: DoEvents: Wend


    Do: Set posts = .Document.getElementsByClassName("product type-product"): DoEvents: Loop Until posts.Length > 0

    For Each post In posts
        With post.getElementsByClassName("woocommerce-loop-product__title")
            If .Length Then
                Cells(counter, 1) = .Item(0).innerText
                Cells(counter, 3) = "Lloyd"
                Cells(counter, 4) = Now()
           End If
        End With
        With post.getElementsByClassName("woocommerce-Price-amount amount")
            If .Length Then Cells(counter, 2) = CDbl(.Item(0).innerText)
        End With
        
        With post.getElementsByTagName("a")
            If .Length Then Cells(counter, 8) = .Item(0).href
        End With
        
        counter = counter + 1
    Next post
    .Quit
End With

我现在正在尝试添加一个代码来捕获库存是否“缺货”,如下所示:

        With post.getElementsByClassName("stock out-of-stock")
            If .Length Then Cells(counter, 6) = .Item(0).innerText
        End With

但这未能确定其缺货并在相关列/行中捕获“缺货”

相关的html部分在这里:

<div class="et_pb_module_inner">
    <p class="stock out-of-stock">Out of stock</p>
    <section class="cwginstock-subscribe-form ">

【问题讨论】:

    标签: html vba web-scraping


    【解决方案1】:

    您正在循环的元素,由以下代码选择,是页面底部列出的相关产品。

    Set posts = .document.getElementsByClassName("product type-product")
    

    此相关项目列表不包含有关库存水平的信息,因此您建议的测试 If .Length 为 False,代码移至 End With

    我不确定您为什么访问此页面,该页面确实保存了主要产品的库存水平信息,并且只继续从同一页面上抓取相关商品信息,而不访问链接页面以从中抓取库存信息。

    【讨论】:

      猜你喜欢
      • 2018-05-22
      • 2014-03-02
      • 1970-01-01
      • 2018-01-09
      • 1970-01-01
      • 2020-12-23
      • 2013-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多