【问题标题】:Using submit_form() from rvest package returns a form which is not updated使用 rvest 包中的 submit_form() 返回一个未更新的表单
【发布时间】:2016-06-16 20:22:31
【问题描述】:

在使用 R(版本 3.3.0)中的 rvest 包(版本 0.3.1)将信息输入表单后,我试图从网站上抓取数据。以下是我的代码:

# Load Packages
library(rvest)

# Specify URL
url <- "http://www.cocorahs.org/ViewData/ListDailyPrecipReports.aspx"
cocorahs <- html_session(url)

# Grab Initial Form
#  Form is filled in stages. Here, only do country and date
form.unfilled <- cocorahs %>% html_node("form") %>% html_form()
form.filled <- form.unfilled %>% 
  set_values("frmPrecipReportSearch:ucStateCountyFilter:ddlCountry" = "840",
             "frmPrecipReportSearch_ucDateRangeFilter_dcStartDate" = "6/15/2016",
             "frmPrecipReportSearch_ucDateRangeFilter_dcEndDate" = "6/15/2016")

submit_form(cocorahs, form.filled,
            submit="frmPrecipReportSearch:btnSearch") %>%
  html_node("form") %>% html_form()

我期待结果显示更新后的表单;当国家更新为美国时,日期范围恢复为默认值(访问日期)。为了确保表单更新该特定字段,我缺少什么?

【问题讨论】:

    标签: r rvest


    【解决方案1】:

    我认为你在

    中犯了一个错误
    "frmPrecipReportSearch:ucStateCountyFilter:ddlCountry" = "840"
    

    当需要国家/地区名称时,您输入了一个数值。

    请看下面的代码

    # Load Packages
    library(rvest)
    
    # Specify URL
    url <- "http://www.cocorahs.org/ViewData/ListDailyPrecipReports.aspx"
    cocorahs <- html_session(url)
    
    # Grab Initial Form
    #  Form is filled in stages. Here, only do country and date
    form.unfilled <- cocorahs %>% html_node("form") %>% html_form()
    form.filled <- form.unfilled %>%
    set_values("frmPrecipReportSearch:ucStationTextFieldsFilter:tbTextFieldValue" = "840",
             "frmPrecipReportSearch_ucDateRangeFilter_dcStartDate" = "6/15/2016",
             "frmPrecipReportSearch_ucDateRangeFilter_dcEndDate" = "6/15/2016")
    
    # submit the form and save as a new session
    session <- submit_form(cocorahs, form.filled) 
    
    # look for a table in the nodes
    table <- session %>% html_nodes("table")
    
    # The table you want
    table[[7]] %>% html_table()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      • 1970-01-01
      • 2021-07-31
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      • 2019-03-03
      相关资源
      最近更新 更多