【问题标题】:Filling and submit search with rvest in R在 R 中使用 rvest 填写并提交搜索
【发布时间】:2021-04-29 08:46:52
【问题描述】:

我正在学习如何填写表格并在 R 中使用rvest 提交,当我想在 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"

任何帮助家伙

【问题讨论】:

    标签: r rvest


    【解决方案1】:

    搜索查询在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"
    

    您将无法使用 rvest 处理此问题,但在手动单击验证码后,您会得到您正在寻找的查询:

    https://stackoverflow.com/search?q=ggplot
    

    可能更容易使用我的other answer

    read_html(paste0('https://stackoverflow.com/search?tab=newest&q=',search))
    

    【讨论】:

    • 查看我的编辑,我检查它现在返回 ggplot
    • searched$url 应该发给我这个网址:https://stackoverflow.com/search?q=ggplot。如您所见,https://stackoverflow.com/questions?q=ggplot url 不是与ggplotsearch 相关的结果。
    • 主要思路是发到`stackoverflow.com/search?q=ggplot.
    猜你喜欢
    • 2017-05-08
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    相关资源
    最近更新 更多