网络抓取选择器小工具和 rvest 的问题

Problem with webscraping selector gadget and rvest

我正在尝试使用 SelectorGadget 和 rvest

https://3g.dxy.cn/newh5/view/pneumonia 抓取数据

我使用以下代码成功地抓取了页面中的一些文本。

library(rvest)
url        <- 'https://3g.dxy.cn/newh5/view/pneumonia'
webpage    <- read_html(url)

TEXT_html <- html_nodes(webpage,'.descText___Ui3tV')
TEXT      <- html_text(TEXT_html)

但是当我尝试 select table 其中最重要的数据(table 中被感染的人数)see selection 使用以下代码

TABLE_html <- html_nodes(webpage,'.areaBlock1___3V3UU p')
TABLE      <- html_text(TABLE_html)

输出是"character 0"

我猜是因为table里面的数据是通过API刷新的,所以看不到,但是我真的不知道怎么解决这个问题

有人有想法吗?非常感谢

在此页面上,数据不是从 API 中单独检索的。它实际上存在于您下载的 html 页面中,但它在脚本标记内采用 JSON 格式,rvest 无法读取它的原因是数据仅添加到 DOM 由 Javascript 在页面加载后。要获取数据,您需要提取和解析 JSON:

library(rvest)
library(tibble)
library(jsonlite)

data <- 'https://3g.dxy.cn/newh5/view/pneumonia'  %>%
        read_html()                               %>%
        html_node('#getAreaStat')                 %>%  # This is the tag containing the JSON 
        html_text()                               %>%  # Get the javascript from the node
        strsplit("(getAreaStat = )|(}catch)")     %>%  # Carve out the JSON
        unlist()                                  %>%  
        `[`(2)                                    %>%  # Unlist and extract the JSON
        fromJSON()                                     # Parse the JSON

现在 data 是一个包含来自 JSON 的所有信息的数据框。然而,data的最后一列实际上是一个城市级数据框的列表。由于它们都具有相同的列名,因此可以使用 rbind 将它们绑定在一起。然后可以从 data 中删除最后一列,这样您就有了一个省级数据的数据框和另一个城市级数据的数据框。

city_data      <- as_tibble(do.call(rbind, data$cities))
province_data  <- as_tibble(data[, -8])

所以province_data看起来有点像这样(中文符号没有复制过来而是出现在R控制台)

province_data
#> # A tibble: 33 x 7
#>    provinceName provinceShortNa~ confirmedCount suspectedCount curedCount deadCount
#>    <chr>        <chr>                     <int>          <int>      <int>     <int>
#>  1 ???       ??                       2714              0         52       100
#>  2 ???       ??                        207              0          4         0
#>  3 ???       ??                        173              0          3         0
#>  4 ???       ??                        168              0          0         1
#>  5 ???       ??                        143              0          0         0
#>  6 ???       ??                        132              0          0         0
#>  7 ???       ??                        106              0          0         0
#>  8 ???       ??                         95              0          0         0
#>  9 ???       ??                         91              0          2         1
#> 10 ???       ??                         90              0          0         0
#> # ... with 23 more rows, and 1 more variable: comment <chr>

和 city_data 看起来像这样(同样,在控制台中正确打印了 cityName)。

#> # A tibble: 329 x 5
#>    cityName confirmedCount suspectedCount curedCount deadCount
#>    <chr>             <int>          <int>      <int>     <int>
#>  1 ??               1590              0         47        85
#>  2 ??                213              0          2         4
#>  3 ??                173              0          0         1
#>  4 ??                114              0          0         3
#>  5 ??                 91              0          0         0
#>  6 ??                 71              0          1         2
#>  7 ??                 70              0          0         0
#>  8 ??                 70              0          0         0
#>  9 ??                 65              0          0         0
#> 10 ??                 57              0          0         0
#> # ... with 319 more rows