将正确的参数传递给RCurl / postForm

时间:2016-12-28 07:59:55

标签: r web-scraping rcurl httr

我试图通过RCurl从国家信息中心下载pdf,但我遇到了一些问题。对于此示例URL,我希望pdf对应于默认设置,除了"报告格式"这应该是" PDF"。当我运行以下脚本时,它会保存与选择其他按钮相关联的文件("父和..." / HMDA - 不是默认值)。我尝试将这些输入元素添加到params,但它没有改变任何内容。有人可以帮我找出问题吗?感谢。

library(RCurl)
curl = getCurlHandle()
curlSetOpt(cookiejar = 'cookies.txt', curl = curl)
params = list(rbRptFormatPDF = 'rbRptFormatPDF')

url = 'https://www.ffiec.gov/nicpubweb/nicweb/OrgHierarchySearchForm.aspx?parID_RSSD=2162966&parDT_END=99991231'
html = getURL(url, curl = curl)
viewstate = sub('.*id="__VIEWSTATE" value="([0-9a-zA-Z+/=]*).*', '\\1', html)
event = sub('.*id="__EVENTVALIDATION" value="([0-9a-zA-Z+/=]*).*', '\\1', html)
params[['__VIEWSTATE']] = viewstate
params[['__EVENTVALIDATION']] = event
params[['btnSubmit']] = 'Submit'
result = postForm(url, .params=params, curl=curl, style='POST')

writeBin( as.vector(result), 'test.pdf')

1 个答案:

答案 0 :(得分:1)

这是否提供了正确的PDF?

library(httr)
library(rvest)
library(purrr)

# setup inane sharepoint viewstate parameters
res <- GET(url = "https://www.ffiec.gov/nicpubweb/nicweb/OrgHierarchySearchForm.aspx",
           query=list(parID_RSSD=2162966, parDT_END=99991231))

# extract them
pg <- content(res, as="parsed")
hidden <- html_nodes(pg, xpath=".//form/input[@type='hidden']") 
params <- setNames(as.list(xml_attr(hidden, "value")), xml_attr(hidden, "name"))

# pile on more params
params <- c(
  params, 
  grpInstitution = "rbCurInst", 
  lbTopHolders = "2961897", 
  grpHMDA = "rbNonHMDA", 
  lbTypeOfInstitution = "-99", 
  txtAsOfDate = "12/28/2016", 
  txtAsOfDateErrMsg = "", 
  lbHMDAYear = "2015", 
  grpRptFormat = "rbRptFormatPDF", 
  btnSubmit = "Submit"
)

# submit the req and save to disk
POST(url = "https://www.ffiec.gov/nicpubweb/nicweb/OrgHierarchySearchForm.aspx",
     query=list(parID_RSSD=2162966, parDT_END=99991231),
     add_headers(Origin = "https://www.ffiec.gov"), 
     body = params, 
     encode = "form", 
     write_disk("/tmp/output.pdf")) -> res2