【问题标题】:How to scrape pop-up text using rvest?如何使用 rvest 抓取弹出文本?
【发布时间】:2021-09-23 07:38:06
【问题描述】:

我想从以下网站抓取信息:https://www.theglobaleconomy.com/download-data.php

如您所见,每个经济变量都有相关的信息框,如图中的那个,当您点击i时会弹出: https://i.stack.imgur.com/E3JRy.png

SelectorGadget 并且对代码的检查表明我应该使用“#definitionBoxText”作为 CSS 选择器,但是当我运行 nodes <- read_html("https://www.theglobaleconomy.com/download-data.php") %>% html_nodes("#definitionBoxText") %>% html_text() 时这不起作用,我没有得到任何回报,只是空白。您能否指导我如何获取这些信息?非常感谢任何帮助!

【问题讨论】:

    标签: r web-scraping rvest


    【解决方案1】:

    看起来#definitionBoxText 的值是在您单击 PHP 脚本的信息图标时生成的。这意味着您将无法抓取该文本,除非您使用 RSelenium 之类的内容并模拟对每个图标的点击。

    另一种方法是按 F12 打开开发人员工具,转到“源”选项卡并保存名为 download-data.php 的文件,其中包含您要查找的所有定义。然后,您可以单独抓取该文件。附上可刮部分的样子:

    <div class="indicatorsName">
        Economic growth: the rate of change of real GDP
    </div>
    
    <div class="infoIcon">
        <div class="showDefinition"
            style="margin: 4px 3px 0; padding: 1px 6px 0;  border-radius: 10px; border: 1px solid #333; color: #333; float: right; font-weight: bold; font-size;10px">
            i
        </div>
    </div>
    
    <div class="clearer"></div>
    
    <div class="definition">
        <b>Economic growth: the rate of change of real GDP</b><br /><br />
        Definition:
        Annual percentage growth rate of GDP at market prices based on constant local currency. Aggregates are based on
        constant 2010 U.S. dollars. GDP is the sum of gross value added by all resident producers in the economy plus any
        product taxes and minus any subsidies not included in the value of the products. It is calculated without making
        deductions for depreciation of fabricated assets or for depletion and degradation of natural resources.
    </div>
    </div>
    

    【讨论】:

    • 定义数据在页面加载时出现,可以使用... %&gt;% html_elements("div.definition") %&gt;% html_text()访问。不需要 RSelenium。
    猜你喜欢
    • 2017-06-26
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    相关资源
    最近更新 更多