我正在做一个项目,我需要从这个网站下载FAFSA的完成数据:https://studentaid.gov/data-center/student/application-volume/fafsa-completion-high-school
我正在使用rvest来对数据进行网络抓取,但是当我尝试在链接上使用函数read_html时,它永远不会读入,最终我不得不停止执行。我可以在其他网站上阅读,所以我不确定这是网站特有的问题还是我做错了什么。到目前为止,我的代码如下:
library(rvest)
fafsa_link <- "https://studentaid.gov/data-center/student/application-volume/fafsa-completion-high-school"
read_html(fafsa_link)任何帮助都将不胜感激!谢谢!
发布于 2021-04-23 23:45:47
用户代理头是必需的。下载链接也以json文件的形式给出。你可以正则表达式出链接(或者解析它们);或者像我所做的,正则表达式出一个链接,然后替换其中的状态代码,以获得额外的下载url (给定的url只是在这方面有所不同)。
library(magrittr)
library(httr)
library(stringr)
data <- httr::GET('https://studentaid.gov/data-center/student/application-volume/fafsa-completion-high-school.json', add_headers("User-Agent" = "Mozilla/5.0")) %>%
content(as = "text")
ca <- data %>% stringr::str_match(': "(.*?CA\\.xls)"') %>% .[2] %>% paste0('https://studentaid.gov', .)
ma <- gsub('CA\\.xls', 'MA\\.xls' ,ca)https://stackoverflow.com/questions/67231726
复制相似问题