Ant*_*ico 5 javascript selenium r web-scraping rvest
我正在寻找一种程序化的方法来抓取archive.gov上的数据文件系列的所有可用文件与R. archives.gov似乎使用javascript.我的目标是捕获每个可用文件的URL以及文件名.
住房抵押贷款披露法案数据文件系列有153个条目
在浏览器中,我可以单击"导出"按钮并获取具有此结构的csv文件:
first_exported_record <-
structure(list(resultType = structure(1L, .Label = "fileUnit", class = "factor"),
creators.0 = structure(1L, .Label = "Federal Reserve System. Board of Governors. Division of Consumer and Community Affairs. ca. 1981- (Most Recent)", class = "factor"),
date = structure(1L, .Label = "1981 - 2013", class = "factor"),
documentIndex = 1L, from.0 = structure(1L, .Label = "Series: Home Mortgage Disclosure Data Files, 1981 - 2013", class = "factor"),
from.1 = structure(1L, .Label = "Record Group 82: Records of the Federal Reserve System, 1913 - 2003", class = "factor"),
location.locationFacility1.0 = structure(1L, .Label = "National Archives at College Park - Electronic Records(RDE)", class = "factor"),
location.locationFacility1.1 = structure(1L, .Label = "National Archives at College Park", class = "factor"),
location.locationFacility1.2 = structure(1L, .Label = "8601 Adelphi Road", class = "factor"),
location.locationFacility1.3 = structure(1L, .Label = "College Park, MD, 20740-6001", class = "factor"),
location.locationFacility1.4 = structure(1L, .Label = "Phone: 301-837-0470", class = "factor"),
location.locationFacility1.5 = structure(1L, .Label = "Fax: 301-837-3681", class = "factor"),
location.locationFacility1.6 = structure(1L, .Label = "Email: cer@nara.gov", class = "factor"),
naId = 18491490L, title = structure(1L, .Label = "Non-restricted Ultimate Loan Application Register (LAR) Data, 2012", class = "factor"),
url = structure(1L, .Label = "https://catalog.archives.gov/id/18491490", class = "factor")), .Names = c("resultType",
"creators.0", "date", "documentIndex", "from.0", "from.1", "location.locationFacility1.0",
"location.locationFacility1.1", "location.locationFacility1.2",
"location.locationFacility1.3", "location.locationFacility1.4",
"location.locationFacility1.5", "location.locationFacility1.6",
"naId", "title", "url"), class = "data.frame", row.names = c(NA,
-1L))
Run Code Online (Sandbox Code Playgroud)
然后在这153个条目中的每一个后面都有文件单元页面,其中有多个文件可供下载.例如,首先导出的记录指向:
https://catalog.archives.gov/id/18491490
但是这两个页面看起来都是javascript,所以我不确定我是否需要像phantomjs或selenium这样的东西,或者是否有一些技巧可以用像rvest这样的简单工具导出目录?
在我知道每个文件的网址时,我可以毫无问题地下载它们:
tf <- tempfile()
download.file( "https://catalog.archives.gov/catalogmedia/lz/electronic-records/rg-082/hmda/233_32LU_TSS.pdf?download=false" , tf , mode = 'wb' )
Run Code Online (Sandbox Code Playgroud)
这个文件名是
"Technical Specifications Summary, 2012 Ultimate LAR."
Run Code Online (Sandbox Code Playgroud)
谢谢!
更新:
具体问题是我如何以编程方式从第一个链接(系列ID)到系列中可供下载的所有文件的标题和网址.我尝试了rvest和httr命令没有任何有用的东西来展示它.:/谢谢
由于记录是通过简单的Ajax请求加载的,因此无需在此处加载和解析页面.
要查看请求,只需通过devtools监视它们,然后选择第一个返回一些JSON的请求.然后使用该jsonlite库向R请求相同的URL.它将自动解析结果.
列出153个条目的所有文件(描述+ URL):
library(jsonlite)
options(timeout=60000) # increase timeout to 60sec (default is 10sec)
json = fromJSON("https://catalog.archives.gov/OpaAPI/iapi/v1?action=search&f.level=fileUnit&f.parentNaId=2456161&q=*:*&offset=0&rows=10000&tabType=all")
ids = json$opaResponse$results$result$naId
for (id in ids) { # each id
json = fromJSON(sprintf("https://catalog.archives.gov/OpaAPI/iapi/v1/id/%s", id))
records = json$opaResponse$content$objects$objects$object
for (r in 1:nrow(records)) { # each record
# prints the file description and URL
print(records[r, 'description'])
print(records[r, '@renditionBaseUrl'])
}
}
Run Code Online (Sandbox Code Playgroud)
来自国家档案和记录管理局编写 API 的人们。
嗨安东尼,
无需刮擦;NARA 的目录有一个开放的 API。如果我理解正确的话,您想要下载“家庭抵押贷款披露数据文件”系列(NAID 2456161)中所有文件单元中的所有媒体文件(我们的目录称之为“对象”)。
API 允许对数据中的任何字段进行字段搜索,因此执行该查询的最佳方法不是使用“parentNaId”之类的搜索参数,而是在该特定字段上进行搜索,即返回父系列所在的所有记录NAID 是 2456161。如果您打开这些文件单元之一以使用标识符查看数据(例如 https://catalog.archives.gov/api/v1?naIds=2580657),您可以看到包含以下内容的字段:父系列称为“description.fileUnit.parentSeries”。因此,您的所有记录文件单元及其对象都将位于 https://catalog.archives.gov/api/v1?description.fileUnit.parentSeries=2456161中。如果只想返回没有文件单元记录的对象,可以添加“&type=object”参数。或者,如果您需要文件单元元数据,您还可以使用“type=description”限制结果,因为每个文件单元记录还包含其子对象的所有数据。看起来有超过 1000 个结果,因此您还需要使用“rows”参数来询问一个查询中的所有结果,或者使用“offset”参数和较小的“rows”值进行分页,因为默认响应仅是前 10 个结果。
在对象元数据中,您将在字段中填写可用于下载媒体的 URL 以及可能感兴趣的其他元数据。例如,请注意,其中一些对象被视为电子记录,如各机构的原始档案记录,而其他对象则是 NARA 创建的技术文档。这在“指示符”字段中注明。
如果您还有任何疑问,请告诉我。
谢谢!多米尼克
| 归档时间: |
|
| 查看次数: |
365 次 |
| 最近记录: |