【发布时间】:2021-12-25 07:29:29
【问题描述】:
我正在尝试使用R 中的rvest 抓取阿根廷总统的演讲。这是我到目前为止编写的代码:
library(purrr)
library(rvest)
library(stringr)
url_president <- "https://www.casarosada.gob.ar/informacion/discursos?page=%d"
# extract relevant data on webpage
map_df(1:2, function(i) {
pg <- read_html(sprintf(url_president, i))
data.frame(
title = html_text(html_nodes(pg, ".category-item-title h3")) %>%
str_trim(),
date = html_text(html_nodes(pg, "time")),
link = html_elements(pg, css = ".category-item-title h3") %>% html_attr('href'),
stringsAsFactors = FALSE
)
}) -> argentina_speeches_pres
这会捕获演讲的标题和日期,但不会捕获链接(我想用它来抓取文本)。有人可以帮我解决这个问题吗?我不确定出了什么问题。
【问题讨论】:
标签: r web-scraping purrr rvest stringr