使用 R rvest 库在 iframe 中抓取表格

Can*_*ice 2 iframe r web-scraping

我很擅长使用 R 的 rvest 库来抓取网站,但我正在努力尝试新的东西。从这个网页 - http://www.naia.org/ViewArticle.dbml?ATCLID=205323044 - 我试图抓取大学的主表。

这是我的代码目前的样子:

NAIA_url = "http://www.naia.org/ViewArticle.dbml?ATCLID=205323044"
NAIA_page = read_html(NAIA_url)

tables = html_table(html_nodes(NAIA_page, 'table'))
# tables returns a length-2 list, however neither of these tables are the table I desire.

# grab the correct iframe node
iframe = html_nodes(NAIA_page, "iframe")[3] 
Run Code Online (Sandbox Code Playgroud)

但是,我正在努力克服这一点。(1) 由于某种原因,调用 html_nodes 并没有抓取我想要的表格。(2) 而且我不确定是否应该抓取 iframe,然后尝试从其中抓取表格。

任何帮助表示赞赏!

yee*_*dle 5

如果嵌入的 iframe 是 html,您可以获取iframe源代码并从那里获取所需的表格。


library(rvest)
#> Loading required package: xml2
library(magrittr)
"http://www.naia.org/ViewArticle.dbml?ATCLID=205323044" %>%
  read_html() %>%
  html_nodes("iframe") %>%
  extract(3) %>% 
  html_attr("src") %>% 
  read_html() %>% 
  html_node("#searchResultsTable") %>% 
  html_table() %>%
  head()
#>                                   College or University       City, State
#> 1                   Central Christian College ATHLETICS     McPherson, KS
#> 2 +                   Crowley's Ridge College ATHLETICS     Paragould, AR
#> 3                       Edward Waters College ATHLETICS  Jacksonville, Fl
#> 4                 Fisher College ADMISSIONS | ATHLETICS        Boston, MA
#> 5       Georgia Gwinnett College ADMISSIONS | ATHLETICS Lawrenceville, GA
#> 6   Lincoln Christian University ADMISSIONS | ATHLETICS       Lincoln, IL
#>   Conference Enrollment
#> 1     A.I.I.        259
#> 2     A.I.I.          0
#> 3     A.I.I.        805
#> 4     A.I.I.        600
#> 5     A.I.I.      9,720
#> 6     A.I.I.      1,060
Run Code Online (Sandbox Code Playgroud)