r - 使用 modify_url 构建多个 URL
问题描述
我创建了一个页面抓取功能来抓取一些数据。我希望能够创建一个 URL 列表,以便我可以在函数调用中传递多个参数以构建不同的 URL。有没有办法做到这一点httr::modify_url
?
我创建单个 URL 的代码如下:
library(tidyverse)
#> Registered S3 methods overwritten by 'ggplot2':
#> method from
#> [.quosures rlang
#> c.quosures rlang
#> print.quosures rlang
library(httr)
# Arguments for Function
hand = NULL
prp = "P"
month = NULL
year = 2019
pitch_type = "FA"
report_type = "pfx"
lim = 0
url <- httr::modify_url("https://legacy.baseballprospectus.com/pitchfx/leaderboards/index.php",
query = list(
hand = hand,
reportType = report_type,
prp = prp,
month = month,
year = year,
pitch = pitch_type,
ds = "velo",
lim = lim
))
# Single Query Result
url
#> [1] "https://legacy.baseballprospectus.com/pitchfx/leaderboards/index.php?reportType=pfx&prp=P&year=2019&pitch=FA&ds=velo&lim=0"
我想知道是否可以使用httr::modify_url
上面的查询和一些组合purrr::reduce(paste0)
来创建 URL 的附加参数:
# Requested Query
pitch_type = c("FA", "SI")
report_type = c("pfx", "outcome")
# URL Generating Function for User inputs
generate_urls <- function(hand = NULL, report_type = c("pfx", "outcome"), prp = "P", month = NULL, year = NULL, pitch_type = c("FA", "SI"), lim = 0) {
# Not sure of what to put in function for modify_url call
}
# Result
"https://legacy.baseballprospectus.com/pitchfx/leaderboards/index.php?reportType=pfx&prp=P&year=2019&pitch=FA&ds=velo&lim=0"
#> [1] "https://legacy.baseballprospectus.com/pitchfx/leaderboards/index.php?reportType=pfx&prp=P&year=2019&pitch=FA&ds=velo&lim=0"
"https://legacy.baseballprospectus.com/pitchfx/leaderboards/index.php?reportType=pfx&prp=P&year=2019&pitch=SI&ds=velo&lim=0"
#> [1] "https://legacy.baseballprospectus.com/pitchfx/leaderboards/index.php?reportType=pfx&prp=P&year=2019&pitch=SI&ds=velo&lim=0"
"https://legacy.baseballprospectus.com/pitchfx/leaderboards/index.php?reportType=outcome&prp=P&year=2019&pitch=FA&ds=velo&lim=0"
#> [1] "https://legacy.baseballprospectus.com/pitchfx/leaderboards/index.php?reportType=outcome&prp=P&year=2019&pitch=FA&ds=velo&lim=0"
"https://legacy.baseballprospectus.com/pitchfx/leaderboards/index.php?reportType=outcome&prp=P&year=2019&pitch=SI&ds=velo&lim=0"
#> [1] "https://legacy.baseballprospectus.com/pitchfx/leaderboards/index.php?reportType=outcome&prp=P&year=2019&pitch=SI&ds=velo&lim=0"
解决方案
这是使用 tidyverse 功能的选项。首先,我们可以定义我们想要遍历的参数空间
params <- list(
hand = NULL,
prp = "P",
year = 2019,
month = NULL,
pitch_type = c("FA", "SI"),
report_type = c("pfx", "outcome"),
lim = 0
)
然后我们可以得到所有的URL
library(tidyverse) # tidyr for crossing(); purrr for pmap(), map_chr()
library(httr)
baseurl <- "https://legacy.baseballprospectus.com/pitchfx/leaderboards/index.php"
crossing(!!!params) %>%
pmap(list) %>%
map_chr( ~modify_url(baseurl, query=.x) )
负责crossing()
获取所有可能的参数组合。然后pmap(list)
将 tibble 的每一行变成自己的列表(这是我们需要传递给 的query=
参数modify_url
。最后我们为每组参数调用 url 生成函数并返回一个字符串。
推荐阅读
- ruby-on-rails - 重定向 PATCH 和 PUT 路由轨道 5 以使用 POST
- mongodb - Mongodb查询根据电影使用拆分的导演数量显示电影数量
- pointers - 在设计时移动 TImage 组件时指针操作无效
- docker - 使用 `docker-compose` 而不是 `docker compose` 的链接
- c# - Automapper ProjectTo membersToExpand 过滤
- node.js - 如何阻止 Puppeteer 覆盖重复下载?
- heroku - 如何在 Heroku 上使用 dash dcc.Upload 和 dcc.Store?
- vue.js - 范围滑块如何在 vue.js 中更改步长
- python - 有没有一种简单的方法可以知道登录是否失败 Django?
- reactjs - 如何使用 axios 标头获取数据?