从R中的aspx网页下载文档
Download documents from aspx web page in R
我正在尝试使用 R 中的 "rvest" 和 "downloader" 包从科罗拉多石油和天然气保护委员会 (COGCC) 自动下载油气井文件。
link 到 table/form 包含特定井的文件是;
http://ogccweblink.state.co.us/results.aspx?id=12337064
"id=12337064" 是井的唯一标识符
表格页面的文件可以点击下载。
下面是一个例子。
http://ogccweblink.state.co.us/DownloadDocument.aspx?DocumentId=3172781
"DocumentID=3172781"是要下载的文档的唯一文档ID。在这种情况下,一个 xlsm 文件。文档页面上的其他文件格式包括 PDF 和 xls。
到目前为止,我已经能够编写代码来下载任何井的任何文档,但仅限于第一页。大多数井的文件都在多页上,我无法下载除第 1 页以外的页面上的文件(所有文件页都相似 URL)
## Extract the document id for document to be downloaded in this case "DIRECTIONAL DATA". Used the SelectorGadget tool to extract the CSS path
library(rvest)
html <- html("http://ogccweblink.state.co.us/results.aspx?id=12337064")
File <- html_nodes(html, "tr:nth-child(24) td:nth-child(4) a")
File <- as(File[[1]],'character')
DocId<-gsub('[^0-9]','',File)
DocId
[1] "3172781"
## To download the document, I use the downloader package
library(downloader)
linkDocId<-paste('http://ogccweblink.state.co.us/DownloadDocument.aspx DocumentId=',DocId,sep='')
download(linkDocId,"DIRECTIONAL DATA" ,mode='wb')
trying URL 'http://ogccweblink.state.co.us/DownloadDocument.aspx?DocumentId=3172781'
Content type 'application/octet-stream' length 33800 bytes (33 KB)
downloaded 33 KB
有谁知道如何修改我的代码以在其他页面上下载文档?
非常感谢!
Em
您必须为第二个查询使用完全相同的 cookie,并同时传递视图状态和验证字段。快速示例:
加载 RCurl
并加载 URL 并保留 cookie:
url <- 'http://ogccweblink.state.co.us/results.aspx?id=12337064'
library(RCurl)
curl <- curlSetOpt(cookiejar = 'cookies.txt', followlocation = TRUE, autoreferer = TRUE, curl = getCurlHandle())
page1 <- getURL(url, curl = curl)
解析 HTML 后提取 VIEWSTATE
和 EVENTVALIDATION
值:
page1 <- htmlTreeParse(page1, useInternal = TRUE)
viewstate <- xpathSApply(page1, '//input[@name = "__VIEWSTATE"]', xmlGetAttr, 'value')
validation <- xpathSApply(page1, '//input[@name = "__EVENTVALIDATION"]', xmlGetAttr, 'value')
使用保存的 cookie 再次查询相同的 URL,提取隐藏的 INPUT
值并请求第二页:
page2 <- postForm(url, curl = curl,
.params = list(
'__EVENTARGUMENT' = 'Page',
'__EVENTTARGET' = 'WQResultGridView',
'__VIEWSTATE' = viewstate,
'__EVENTVALIDATION' = validation))
从第二页显示的 table 中提取 URLs:
page2 <- htmlTreeParse(page2, useInternal = TRUE)
xpathSApply(page2, '//td/font/a', xmlGetAttr, 'href')
我正在尝试使用 R 中的 "rvest" 和 "downloader" 包从科罗拉多石油和天然气保护委员会 (COGCC) 自动下载油气井文件。
link 到 table/form 包含特定井的文件是; http://ogccweblink.state.co.us/results.aspx?id=12337064
"id=12337064" 是井的唯一标识符
表格页面的文件可以点击下载。 下面是一个例子。 http://ogccweblink.state.co.us/DownloadDocument.aspx?DocumentId=3172781
"DocumentID=3172781"是要下载的文档的唯一文档ID。在这种情况下,一个 xlsm 文件。文档页面上的其他文件格式包括 PDF 和 xls。
到目前为止,我已经能够编写代码来下载任何井的任何文档,但仅限于第一页。大多数井的文件都在多页上,我无法下载除第 1 页以外的页面上的文件(所有文件页都相似 URL)
## Extract the document id for document to be downloaded in this case "DIRECTIONAL DATA". Used the SelectorGadget tool to extract the CSS path
library(rvest)
html <- html("http://ogccweblink.state.co.us/results.aspx?id=12337064")
File <- html_nodes(html, "tr:nth-child(24) td:nth-child(4) a")
File <- as(File[[1]],'character')
DocId<-gsub('[^0-9]','',File)
DocId
[1] "3172781"
## To download the document, I use the downloader package
library(downloader)
linkDocId<-paste('http://ogccweblink.state.co.us/DownloadDocument.aspx DocumentId=',DocId,sep='')
download(linkDocId,"DIRECTIONAL DATA" ,mode='wb')
trying URL 'http://ogccweblink.state.co.us/DownloadDocument.aspx?DocumentId=3172781'
Content type 'application/octet-stream' length 33800 bytes (33 KB)
downloaded 33 KB
有谁知道如何修改我的代码以在其他页面上下载文档?
非常感谢!
Em
您必须为第二个查询使用完全相同的 cookie,并同时传递视图状态和验证字段。快速示例:
加载
RCurl
并加载 URL 并保留 cookie:url <- 'http://ogccweblink.state.co.us/results.aspx?id=12337064' library(RCurl) curl <- curlSetOpt(cookiejar = 'cookies.txt', followlocation = TRUE, autoreferer = TRUE, curl = getCurlHandle()) page1 <- getURL(url, curl = curl)
解析 HTML 后提取
VIEWSTATE
和EVENTVALIDATION
值:page1 <- htmlTreeParse(page1, useInternal = TRUE) viewstate <- xpathSApply(page1, '//input[@name = "__VIEWSTATE"]', xmlGetAttr, 'value') validation <- xpathSApply(page1, '//input[@name = "__EVENTVALIDATION"]', xmlGetAttr, 'value')
使用保存的 cookie 再次查询相同的 URL,提取隐藏的
INPUT
值并请求第二页:page2 <- postForm(url, curl = curl, .params = list( '__EVENTARGUMENT' = 'Page', '__EVENTTARGET' = 'WQResultGridView', '__VIEWSTATE' = viewstate, '__EVENTVALIDATION' = validation))
从第二页显示的 table 中提取 URLs:
page2 <- htmlTreeParse(page2, useInternal = TRUE) xpathSApply(page2, '//td/font/a', xmlGetAttr, 'href')