在 rvest 中提交没有提交按钮的表单 [英] Submit form with no submit button in rvest

查看:46
本文介绍了在 rvest 中提交没有提交按钮的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个爬虫来下载一些信息,类似于 this Stack Overflow post. 答案对于创建填写的表单很有用,但我正在努力寻找一种在提交时提交表单的方法按钮不是表单的一部分.下面是一个例子:

I'm trying write a crawler to download some information, similar to this Stack Overflow post. The answer is useful for creating the filled-in form, but I'm struggling to find a way to submit the form when a submit button is not part of the form. Here is an example:

session <- html_session("www.chase.com")
form <- html_form(session)[[3]]

filledform <- set_values(form, `user_name` = user_name, `usr_password` = usr_password)
session <- submit_form(session, filledform)

此时,我收到此错误:

Error in names(submits)[[1]] : subscript out of bounds

如何提交此表单?

推荐答案

这是一个对我有用的肮脏的 hack:在研究了 submit_form 源代码,我想我可以通过在表单的代码版本中注入一个假的提交按钮来解决这个问题,然后 submit_form 函数会调用它.它可以工作,只是它给出了一个警告,通常会列出一个不适当的输入对象(但不在下面的示例中).但是,尽管有警告,但代码对我有用:

Here's a dirty hack that works for me: After studying the submit_form source code, I figured that I could work around the problem by injecting a fake submit button into my code version of the form, and then the submit_form function would call that. It works, except that it gives a warning that often lists an inappropriate input object (not in the example below, though). However, despite the warning, the code works for me:

session <- html_session("www.chase.com")
form <- html_form(session)[[3]]

# Form on home page has no submit button,
# so inject a fake submit button or else rvest cannot submit it.
# When I do this, rvest gives a warning "Submitting with '___'", where "___" is
# often an irrelevant field item.
# This warning might be an rvest (version 0.3.2) bug, but the code works.
fake_submit_button <- list(name = NULL,
                           type = "submit",
                           value = NULL,
                           checked = NULL,
                           disabled = NULL,
                           readonly = NULL,
                           required = FALSE)
attr(fake_submit_button, "class") <- "input"
form[["fields"]][["submit"]] <- fake_submit_button

user_name <- "user"
usr_password <- "password"

filledform <- set_values(form, `user_name` = user_name, `usr_password` = usr_password)
session <- submit_form(session, filledform)

成功的结果显示以下警告,我只是忽略:

The successful result displays the following warning, which I simply ignore:

> Submitting with 'submit'

这篇关于在 rvest 中提交没有提交按钮的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆