【问题标题】:VBA scraping generated content which is not in the HTML source fileVBA 抓取生成的不在 HTML 源文件中的内容
【发布时间】:2015-06-07 23:13:51
【问题描述】:

我制作了一个 VBA 代码来从网页上抓取一些产品价格。

我可以让它适用于我想要的所有网站,除了一个。在这个特定的网站上,价格没有显示在页面的 HTML 代码中,但据我搜索网络了解,它们是由 JavaScript 在本地生成的。

我怎样才能获取这些价格?

我正在使用这种方法获取页面源,然后我解析响应文本以获取我需要的信息,例如产品名称和链接,但我无法获取价格:

Set xhr = New MSXML2.XMLHTTP60

        adresa = "http://www.elefant.ro/carti/natura"
        With xhr

            .Open "GET", adresa, False
            .send

            If .readyState = 4 And .Status = 200 Then
                Set doc = New MSHTML.HTMLDocument
                corp = .responseText
            Else
                Cells(i, 2) = "error"
            End If
        End With

这是我得到的页面的 HTML 代码的一部分,价格应该是:

<div class="js_PriceContainer" style="display: none">
            <div>
                <div class="product_old_price js_oldPrice">
                    <a class="FFTracking" rel=""  href="/carti/natura/resurse-naturale/ghidul-culegatorului-de-ciuperci-211009.html"><span class="js_basePrice"></span> lei</a>
                </div>

                <div class="product_discount js_ProductDiscount">
                    <a class="FFTracking" rel=""  href="/carti/natura/resurse-naturale/ghidul-culegatorului-de-ciuperci-211009.html">(<span class="js_discount"></span>%)</a>
                </div>
            </div>

            <!-- <div class="promo_bf">
                                            <div class="product_old_price"></div>
                        <div class="product_discount"></div>
                                    </div> -->

            <div style="clear:both"></div>
            <div class="product_final_price js_FinalPriceContainer">
                <a class="FFTracking" rel=""  href="/carti/natura/resurse-naturale/ghidul-culegatorului-de-ciuperci-211009.html">
                    <span class="js_finalPrice js_promoNonBf"></span>
                    <span class="js_finalPrice js_promoBf"></span>
                </a>
            </div>
        </div>

请帮忙! 谢谢!

【问题讨论】:

  • 如果您需要读取客户端生成的动态内容,那么您应该(例如)自动化 IE 加载页面并使用自动化读取内容。如果您使用 IE Developer 工具,那么您应该能够查看呈现的内容(而不是仅从服务器交付的 HTML)

标签: javascript vba web-scraping


【解决方案1】:

在您的项目中导入QT WebKit,并使用webkit 的强大功能在抓取之前呈现网页。 This guide 解释了如何在 LXML scraper 中实现 webkit。

【讨论】:

  • 谢谢,但读完quide后我可以告诉你,这超出了我的业余程序员技能……有更简单的版本吗?
  • 如果这不是一个编程项目,那么现成的爬虫不是很有用吗?
  • 我更喜欢编程,所以我也可以学到一些东西。
猜你喜欢
  • 2020-07-01
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 2022-01-24
  • 2014-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多