gpt4 book ai didi

r - 在 R 中使用 rvest 填充和提交搜索

转载 作者:行者123 更新时间:2023-12-03 13:39:52 24 4
gpt4 key购买 nike

我正在学习如何填写表格和提交 rvest在 R 中,当我想在 stackoverflow 中搜索 ggplot 标签时,我陷入了困境。这是我的代码:

url<-"https://stackoverflow.com/questions"

(session<-html_session("https://stackoverflow.com/questions"))

(form<-html_form(session)[[2]])
(filled_form<-set_values(form, tagQuery = "ggplot"))
searched<-submit_form(session, filled_form)
我有错误:
Submitting with '<unnamed>'
Error in parse_url(url) : length(url) == 1 is not TRUE
按照这个问题( rvest error on form submission )我尝试了几件事来解决这个问题,但我不能:
filled_form$fields[[13]]$name<-"submit"
filled_form$fields[[14]]$name<-"submit"
filled_form$fields[[13]]$type<-"button"
filled_form$fields[[14]]$type<-"button"
任何帮助家伙

最佳答案

搜索查询在 html_form(session)[[1]]因为没有 submit这种形式的按钮:

<form> 'search' (GET /search)
<input text> 'q':
workaround似乎工作:
<form> 'search' (GET /search)
<input text> 'q':
<input submit> '':
给出以下代码序列:
library(rvest)
url<-"https://stackoverflow.com/questions"
(session<-html_session("https://stackoverflow.com/questions"))
(form<-html_form(session)[[1]])

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
(filled_form<-set_values(form, q = "ggplot"))


searched<-submit_form(session, filled_form)
问题是回复有验证码:
searched$url
[1] "https://stackoverflow.com/nocaptcha?s=7291e7e6-9b8b-4b5f-bd1c-0f6890c23573"
enter image description here
您将无法使用 rvest 处理此问题,但在验证码上手动单击后,您会得到您正在寻找的查询:
https://stackoverflow.com/search?q=ggplot
可能更容易使用我的 other answer和:
read_html(paste0('https://stackoverflow.com/search?tab=newest&q=',search))

关于r - 在 R 中使用 rvest 填充和提交搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65886589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com