小编Joh*_*ohn的帖子

使用 r 导航和抓取带有下拉 html 表单的网页

我试图从http://www.footballoutsiders.com/stats/snapcounts抓取数据,但我无法更改网站下拉框中的字段(“团队”、“周”、“位置”) ,和“年”)。我试图用 rvest 抓取与 team = "ALL", week = "1", pos = "All", and year = "2015" 相关的表格如下。

url <- "http://www.footballoutsiders.com/stats/snapcounts"
pgsession <- html_session(url)
pgform <-html_form(pgsession)[[3]]
filled_form <-set_values(pgform,
            "team" = "ALL",
            "week" = "1",
            "pos"  = "ALL",
            "year" = "2015"             
 )

 submit_form(session=pgsession,form=filled_form, POST=url)

 y <- read_html("http://www.footballoutsiders.com/stats/snapcounts")

 y <- y %>%
    html_nodes("table") %>%
    .[[2]] %>%
    html_table(header=TRUE)
Run Code Online (Sandbox Code Playgroud)

此代码返回与下拉框中的默认变量相关联的表,即 team = "ALL"、week = "20"、pos = "QB" 和 year = "2015",这是一个仅包含 11 个观测值的数据框。如果它真的改变了字段,它会返回一个包含 1,695 个观测值的数据框。

post r dataframe web-scraping rvest

5
推荐指数
1
解决办法
3490
查看次数

标签 统计

dataframe ×1

post ×1

r ×1

rvest ×1

web-scraping ×1