【问题标题】:Scraping a table using the rvest package使用 rvest 包抓取表格
【发布时间】:2017-12-27 16:31:30
【问题描述】:

我对网络抓取完全不熟悉,我正在探索 R 中 rvest 库的潜力。

我正在尝试从以下website 中获取有关意大利各省幸福感的表格,

install.packages('rvest') 

library('rvest')

url <- 'http://www.ilsole24ore.com/speciali/qvita_2017_dati/home.shtml'

webpage <- read_html(url)

但我无法识别表的 XPath。

【问题讨论】:

  • 这个站点有一个javascript生成的表,所以你将无法抓取它,除非你使用像phantomjs这样的外部工具......检查以下站点datacamp.com/community/tutorials/…

标签: r web-scraping rvest


【解决方案1】:

即使有了以下内容,您还有很多工作要做。 HTML 的形状很糟糕。

library(rvest)
library(stringi)
library(tidyverse)

read_html("http://www.ilsole24ore.com/speciali/qvita_2017_dati/home.shtml") %>%  # get the main site
  html_node(xpath=".//script[contains(., 'goToDefaultPage')]") %>%               # find the <script> block that dynamically loads the page
  html_text() %>%
  stri_match_first_regex("goToDefaultPage\\('(.*)'\\)") %>%                      # extract the page link
  .[,2] %>% 
  sprintf("http://www.ilsole24ore.com/speciali/qvita_2017_dati/%s", .) %>%       # prepend the URL prefix
  read_html() -> actual_page                                                     # get the dynamic page

tab <- html_nodes(actual_page, xpath=".//table")[[2]]                            # find the actual data table

一旦你这样做了^^,你就有了一个HTML &lt;table&gt;。它的形式非常糟糕、糟糕、可悲,该网站应该为它滥用 HTML 的方式感到羞耻。

继续尝试html_table()。太糟糕了,它打破了httr

我们需要逐行攻击它,并且需要一个辅助函数,以免 R 代码看起来很糟糕:

`%|0%` <- function(x, y) { if (length(x) == 0) y else x }

^^ 将帮助我们用空白 "" 填充类似 NULL 的内容。

现在,我们逐行提取我们需要的 &lt;td&gt; 值。这并没有得到所有这些数据,因为我不需要这些数据并且它需要清理,我们稍后会看到;

html_nodes(tab, "tr") %>% 
  map_df(~{
    list(
      posizione = html_text(html_nodes(.x, xpath=".//td[2]"), trim=TRUE) %|0% "",
      diff_pos = html_text(html_nodes(.x, xpath=".//td[5]"), trim=TRUE) %|0% "",
      provincia = html_text(html_nodes(.x, xpath=".//td[8]"), trim=TRUE) %|0% "",
      punti = html_text(html_nodes(.x, xpath=".//td[11]"), trim=TRUE) %|0% "",
      box1 = html_text(html_nodes(.x, xpath=".//td[14]"), trim=TRUE) %|0% "",
      box2 = html_text(html_nodes(.x, xpath=".//td[17]"), trim=TRUE) %|0% "",
      box3 = html_text(html_nodes(.x, xpath=".//td[20]"), trim=TRUE) %|0% ""
    )
  })
## # A tibble: 113 x 7
##     posizione             diff_pos            provincia                 punti  box1  box2  box3
##         <chr>                <chr>                <chr>                 <chr> <chr> <chr> <chr>
##  1            Lavoro e Innovazione                      Giustizia e Sicurezza                  
##  2 Diff. pos.                                                                                  
##  3          1                    3              Belluno                   583                  
##  4          2                   -1                Aosta                   578     9    63    22
##  5          3                    2              Sondrio                   574     4    75     1
##  6          4                    3              Bolzano                   572     2     4     7
##  7          5                   -2               Trento                   567     8    11    15
##  8          6                    4              Trieste                   563     6    10     2
##  9          7                    9 Verbano-Cusio-Ossola                   548    18    73    25
## 10          8                   -6               Milano                   544     1     2    10
## # ... with 103 more rows

如您所见,它遗漏了一些内容,并且在标题中有一些垃圾,但您比以前更进一步了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多