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

时间:2020-01-28 11:38:11

标签: r web-scraping screen-scraping 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)

但是当我尝试使用以下代码选择最重要的数据(表中被感染的人数)see selection的表时

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

输出为"character 0"

我想这是因为表中的数据由于通过API刷新而无法看到,但我真的不知道如何解决这个问题

有人有想法吗?非常感谢你

1 个答案:

答案 0 :(得分:3)

在此页面上,未单独从API检索数据。它实际上存在于您下载的html页面中,但在script标签内为JSON格式,而rvest无法读取它的原因是,该数据仅在页面加载后才通过Javascript添加到DOM中。要获取数据,您需要提取并解析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