首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用rvest包抓取表

使用rvest包抓取表
EN

Stack Overflow用户
提问于 2017-12-28 00:31:31
回答 1查看 578关注 0票数 1

我对web scraping完全陌生,我正在探索R中的rvest库的潜力。

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

代码语言:javascript
复制
install.packages('rvest') 

library('rvest')

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

webpage <- read_html(url)

但是我无法确定表的XPath。

EN

回答 1

Stack Overflow用户

发布于 2017-12-28 04:31:04

即使有了以下内容,您也有相当多的工作要做。HTML的状态很糟糕。

代码语言:javascript
复制
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

一旦你这样做了^^,你就有了一个超文本标记语言<table>。它是一个可怕的、糟糕的、可悲的网站,它应该为它滥用HTML而感到羞耻。

去试试html_table()吧。这太糟糕了,以至于把httr弄坏了。

我们需要逐行攻击它,并且需要一个辅助函数soas才不会让R代码看起来很可怕:

代码语言:javascript
复制
`%|0%` <- function(x, y) { if (length(x) == 0) y else x }

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

现在,我们逐行执行,提取所需的<td>值。这不会得到所有的数据,因为我不需要这些数据,而且它需要清理,我们稍后会看到;

代码语言:javascript
复制
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

正如您所看到的,它遗漏了一些东西,并且在标题中有一些垃圾,但您比以前走得更远。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47995544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档