使用rvest填写搜索表单并下载附件

使用rvest填写搜索表单并下载附件,r,web-scraping,rvest,R,Web Scraping,Rvest,我正在尝试使用rvest获取劳工部的数据。我有一个EIN和PN(网络搜索表单中的参数)列表,我想通过它们进行搜索。以下是我目前掌握的情况: library(rvest) library(magrittr) ## URL to page with search form to be populated site <- "http://www.efast.dol.gov/portal/app/disseminate?execution=e1s1" session <- html_se

我正在尝试使用rvest获取劳工部的数据。我有一个EIN和PN(网络搜索表单中的参数)列表,我想通过它们进行搜索。以下是我目前掌握的情况:

library(rvest)
library(magrittr)

## URL to page with search form to be populated
site <- "http://www.efast.dol.gov/portal/app/disseminate?execution=e1s1"

session <- html_session(site)

form <- session %>%
  html_nodes("form") %>%
  extract2(1) %>%
  html_form() %>%
  set_values(`ein` = "060646973", # example EIN
             `pn` = "001") # example PN

result <- submit_form(session, form)
库(rvest)
图书馆(magrittr)
##指向要填充搜索表单的页面的URL
站点%
html_表单()%>%
设置值(`ein`=“060646973”,示例ein)
`pn`=“001”)#示例pn

结果这并不能解决您的问题(有大量的RSelenium-SO回复和博客帖子帮助您使用RSelenium),但您必须回答的“为什么”对于这个网站来说很难看(它提供了一个指针,指示您必须从哪里开始使用RSelenium方法)

该站点在服务器端使用“JavaServerFaces”和javascript来维护状态和增强导航。实际上,您必须从开始,这样后端才能正确启动会话

填写这两个字段后,它会发出一个类似以下内容的
POST
请求(以“复制为卷曲”格式):

我发布它是为了让您看到它提交的一些附加字段,这些字段最初并不直接在

POST
的响应类似于:

HTTP/1.1 200 OK
X-Powered-By: Servlet/3.0
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: no-cache
Cache-Control: no-store
X-Powered-By: JSF/2.0
X-Powered-By: JSF/2.0
X-UA-Compatible: IE=EmulateIE7
Content-Type: application/xml; charset=UTF-8
Content-Language: en-US
Date: Fri, 23 Dec 2016 13:10:26 GMT
Content-Length: 142
Connection: keep-alive

<?xml version='1.0' encoding='UTF-8'?>
<partial-response><redirect url="/portal/app/disseminate?execution=e1s2"></redirect></partial-response>
HTTP/1.1200正常
X-Powered-By:Servlet/3.0
Pragma:没有缓存
到期时间:1970年1月1日星期四格林威治标准时间00:00:00
缓存控制:没有缓存
缓存控制:没有存储
X-Powered-By:JSF/2.0
X-Powered-By:JSF/2.0
X-UA-Compatible:IE=仿真E7
内容类型:application/xml;字符集=UTF-8
内容语言:en US
日期:2016年12月23日星期五13:10:26 GMT
内容长度:142
连接:保持活力

这是一个Java服务器面临AJAX重定向响应,最终导致您被重定向到结果页面,其中包含
中的实际结果。您是否可以共享其他搜索参数,如归档ID、确认ID。您需要
RSelenium
来填写表单,如果你能单独用
rvest
来做这件事,我会很惊讶。
HTTP/1.1 200 OK
X-Powered-By: Servlet/3.0
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: no-cache
Cache-Control: no-store
X-Powered-By: JSF/2.0
X-Powered-By: JSF/2.0
X-UA-Compatible: IE=EmulateIE7
Content-Type: application/xml; charset=UTF-8
Content-Language: en-US
Date: Fri, 23 Dec 2016 13:10:26 GMT
Content-Length: 142
Connection: keep-alive

<?xml version='1.0' encoding='UTF-8'?>
<partial-response><redirect url="/portal/app/disseminate?execution=e1s2"></redirect></partial-response>