使用 rvest 在 R 中解析 table 和 url

Parsing table and urls in R with rvest

抱歉再问一个问题。

我需要来自这个 table 的数据:http://rspp.ru/tables/non-financial-reports-library/ 它包含俄罗斯公司的非财务报告。刮掉它是合法的。为了研究目的,我需要做一些文本挖掘。

理想情况下,我需要以下输出:公司 - 年 - 报告 URL。

我正在尝试抓取它,但我无法将 URLs 对应到公司和年份数据。这是我的脚本:

library(rvest)
library(dplyr)

url = "http://rspp.ru/tables/non-financial-reports-library/"

page = read_html(url)

# table
tab = page %>% 
  html_node("table") %>% 
  html_table(fill = T) 

# links
links = page %>% 
  html_node("table") %>% 
  html_nodes("a") %>% 
  html_attr("href")

你能帮忙吗?

table不规则。一种丑陋的方法是分别在列和行中使用 colspanrowspan 属性值来重建 table,以将 table 扩展为常规数据框。

然后您可以添加适当的 header 并考虑到合并的单元格,我只是在适用年份重复相同的 url。我确实抓取了给定报告所涵盖年份的文本描述,例如2007-2009(在带有链接的单元格中看到),但不要将其输出为 header 行中使用的年份。

library(rvest)
library(stringr)

url <- 'http://rspp.ru/tables/non-financial-reports-library/'
page <- read_html(url)
headers <- page %>% html_nodes('.company-report-table .register-table__row:nth-child(1) th')%>%html_text()
companies <- page %>% html_nodes('.company-report-table .register-table__row td:nth-child(1) span')%>%html_text()
body_rows <- page %>% html_nodes('.register-table__row ~ .register-table__row')
df <- data.frame(matrix(NA_character_, nrow = length(body_rows), ncol = length(headers)))
n <- 0

for(row in seq_along(body_rows)){
  curr_row <-  body_rows[[row]] 
  rspan <- curr_row %>% html_node('td') %>% html_attr('rowspan') %>% as.integer() #rspan tells us how many rows per company
  
  if(!is.na(rspan)){
    n <- n + 1
    title <- companies[[n]]
  }
  df[row,1] = title 
  # handle other columns excluding first
  columns_minus_first <- curr_row %>% html_nodes('td:not(:nth-child(1))') # not always 21 range 10 > 21 but we use colspan to expand to 21
  c <- 1
  
  for(column in seq_along(columns_minus_first)){
    curr_col <- columns_minus_first[[column]]
    cspan <- curr_col %>% html_attr('colspan') %>% as.integer() #use cspan value to determine how many years report covers
    
    if(!is.na(cspan)){
      link <- paste0('http://rspp.ru', curr_col %>% html_node('a') %>% html_attr('href'))
      year <- str_extract(curr_col %>% html_text() ,'\b[0-9-]{4,9}\b') #purists may want a tighter regex for year spans
      
      for(i in seq_along(cspan)){ #we will start writing out from col 2 as first col is the company name
        df[row,i+c] <- link #repeats for each year covered by report (could alter this for only first)
      }
    }
    c <- c + 1
  }
}

colnames(df) <- headers
df <- tibble(df)