rvest 在 R 中使用表单输入进行 Webscraping
rvest Webscraping in R with form inputs
我无法解决 R 中的这个问题,如果你能在这里给我一些建议,我将不胜感激。
我正在尝试从 https://www.investing.com/rates-bonds/spain-5-year-bond-yield-historical-data 中抓取历史债券收益率数据,仅供个人使用(当然)。
此处提供的解决方案非常有效,但只能抓取每日数据的前 24 个时间戳:
我想要实现的是更改日期范围以抓取更多历史数据。
基于SelectorGadget工具,日期范围的输入表单id调用//*[(@id = "widgetFieldDateRange")]
我也尝试过使用以下代码行来更改日期值但没有成功:
library(rvest)
url1 <- "https://www.investing.com/rates-bonds/spain-5-year-bond-yield-historical-data" #Spain 5yr yield
session <- html_session(url1)
pgform <- html_form(session)[[1]]
pgform$fields[[3]]$value <- "01/01/2010 - 09/10/2020"
result <- submit_form(session, pgform)
问题:知道如何正确提交新日期范围并检索扩展时间序列吗?
非常感谢您的帮助!
PS:很遗憾,URL 不会根据日期范围发生变化。
您可以直接执行 POST 请求:
POST https://www.investing.com/instruments/HistoricalDataAjax
您需要从请求中所需的页面中抓取一些信息:
- 来自
div
标签的 pair_ids
属性
- 来自
h2
标签的 header 值 .instrumentHeader
class
完整代码:
library(rvest)
library(httr)
startDate <- as.Date("2020-06-01")
endDate <- Sys.Date() #today
userAgent <- "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"
mainUrl <- "https://www.investing.com/rates-bonds/spain-5-year-bond-yield-historical-data"
s <- html_session(mainUrl)
pair_ids <- s %>%
html_nodes("div[pair_ids]") %>%
html_attr("pair_ids")
header <- s %>% html_nodes(".instrumentHeader h2") %>% html_text()
resp <- s %>% rvest:::request_POST(
"https://www.investing.com/instruments/HistoricalDataAjax",
add_headers('X-Requested-With'= 'XMLHttpRequest'),
user_agent(userAgent),
body = list(
curr_id = pair_ids,
header = header[[1]],
st_date = format(startDate, format="%m/%d/%Y"),
end_date = format(endDate, format="%m/%d/%Y"),
interval_sec = "Daily",
sort_col = "date",
sort_ord = "DESC",
action = "historical_data"
),
encode = "form") %>%
html_table
print(resp[[1]])
输出:
Date Price Open High Low Change %
1 Oct 09, 2020 -0.339 -0.338 -0.333 -0.361 2.42%
2 Oct 08, 2020 -0.331 -0.306 -0.306 -0.338 7.47%
3 Oct 07, 2020 -0.308 -0.323 -0.300 -0.324 -0.65%
4 Oct 06, 2020 -0.310 -0.288 -0.278 -0.319 7.27%
5 Oct 05, 2020 -0.289 -0.323 -0.278 -0.331 -10.39%
6 Oct 03, 2020 -0.322 -0.322 -0.322 -0.322 1.42%
7 Oct 02, 2020 -0.318 -0.311 -0.302 -0.320 5.65%
.....................................................
.....................................................
96 Jun 08, 2020 -0.162 -0.152 -0.133 -0.173 13.29%
97 Jun 05, 2020 -0.143 -0.129 -0.127 -0.154 13.49%
98 Jun 04, 2020 -0.126 -0.089 -0.063 -0.148 38.46%
99 Jun 03, 2020 -0.091 -0.120 -0.087 -0.128 -35.00%
100 Jun 02, 2020 -0.140 -0.148 -0.137 -0.166 14.75%
101 Jun 01, 2020 -0.122 -0.140 -0.101 -0.150 -17.57%
如果您替换 mainUrl
变量的值,这也适用于任何页面,例如 this one
我无法解决 R 中的这个问题,如果你能在这里给我一些建议,我将不胜感激。
我正在尝试从 https://www.investing.com/rates-bonds/spain-5-year-bond-yield-historical-data 中抓取历史债券收益率数据,仅供个人使用(当然)。
此处提供的解决方案非常有效,但只能抓取每日数据的前 24 个时间戳:
我想要实现的是更改日期范围以抓取更多历史数据。
基于SelectorGadget工具,日期范围的输入表单id调用//*[(@id = "widgetFieldDateRange")]
我也尝试过使用以下代码行来更改日期值但没有成功:
library(rvest)
url1 <- "https://www.investing.com/rates-bonds/spain-5-year-bond-yield-historical-data" #Spain 5yr yield
session <- html_session(url1)
pgform <- html_form(session)[[1]]
pgform$fields[[3]]$value <- "01/01/2010 - 09/10/2020"
result <- submit_form(session, pgform)
问题:知道如何正确提交新日期范围并检索扩展时间序列吗?
非常感谢您的帮助!
PS:很遗憾,URL 不会根据日期范围发生变化。
您可以直接执行 POST 请求:
POST https://www.investing.com/instruments/HistoricalDataAjax
您需要从请求中所需的页面中抓取一些信息:
- 来自
div
标签的pair_ids
属性 - 来自
h2
标签的 header 值.instrumentHeader
class
完整代码:
library(rvest)
library(httr)
startDate <- as.Date("2020-06-01")
endDate <- Sys.Date() #today
userAgent <- "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"
mainUrl <- "https://www.investing.com/rates-bonds/spain-5-year-bond-yield-historical-data"
s <- html_session(mainUrl)
pair_ids <- s %>%
html_nodes("div[pair_ids]") %>%
html_attr("pair_ids")
header <- s %>% html_nodes(".instrumentHeader h2") %>% html_text()
resp <- s %>% rvest:::request_POST(
"https://www.investing.com/instruments/HistoricalDataAjax",
add_headers('X-Requested-With'= 'XMLHttpRequest'),
user_agent(userAgent),
body = list(
curr_id = pair_ids,
header = header[[1]],
st_date = format(startDate, format="%m/%d/%Y"),
end_date = format(endDate, format="%m/%d/%Y"),
interval_sec = "Daily",
sort_col = "date",
sort_ord = "DESC",
action = "historical_data"
),
encode = "form") %>%
html_table
print(resp[[1]])
输出:
Date Price Open High Low Change %
1 Oct 09, 2020 -0.339 -0.338 -0.333 -0.361 2.42%
2 Oct 08, 2020 -0.331 -0.306 -0.306 -0.338 7.47%
3 Oct 07, 2020 -0.308 -0.323 -0.300 -0.324 -0.65%
4 Oct 06, 2020 -0.310 -0.288 -0.278 -0.319 7.27%
5 Oct 05, 2020 -0.289 -0.323 -0.278 -0.331 -10.39%
6 Oct 03, 2020 -0.322 -0.322 -0.322 -0.322 1.42%
7 Oct 02, 2020 -0.318 -0.311 -0.302 -0.320 5.65%
.....................................................
.....................................................
96 Jun 08, 2020 -0.162 -0.152 -0.133 -0.173 13.29%
97 Jun 05, 2020 -0.143 -0.129 -0.127 -0.154 13.49%
98 Jun 04, 2020 -0.126 -0.089 -0.063 -0.148 38.46%
99 Jun 03, 2020 -0.091 -0.120 -0.087 -0.128 -35.00%
100 Jun 02, 2020 -0.140 -0.148 -0.137 -0.166 14.75%
101 Jun 01, 2020 -0.122 -0.140 -0.101 -0.150 -17.57%
如果您替换 mainUrl
变量的值,这也适用于任何页面,例如 this one