【发布时间】:2020-08-31 20:49:11
【问题描述】:
我目前正在对新闻杂志进行网络抓取,但不幸的是,我不知道如何建立工作队列。我只能在一个页面上抓取所有文章的内容,但我想要一个队列,它会自动对其余文章执行相同的操作。
library(rvest)
library(tidyverse)
library(data.table)
library(plyr)
library(writexl)
map_dfc(.x = c("em.entrylist__title", "time.entrylist__time"),
.f = function(x) {read_html("https://www.sueddeutsche.de/news/page/1?search=Corona&sort=date&all%5B%5D=dep&all%5B%5D=typ&all%5B%5D=sys&time=2020-07-19T00%3A00%2F2020-07-27T23%3A59&startDate=27.07.2020&endDate=01.08.2020") %>%
html_nodes(x) %>%
html_text()}) %>%
bind_cols(url = read_html("https://www.sueddeutsche.de/news/page/1?search=Corona&sort=date&all%5B%5D=dep&all%5B%5D=typ&all%5B%5D=sys&time=2020-07-19T00%3A00%2F2020-07-27T23%3A59&startDate=27.07.2020&endDate=01.08.2020") %>%
html_nodes("a.entrylist__link") %>%
html_attr("href")) %>%
setNames(nm = c("title", "time", "url")) -> temp
map_df(.x = temp$url[1:50],
.f = function(x){tibble(url = x,
text = read_html(x) %>%
html_nodes("#article-app-container > article > div.css-isuemq.e1lg1pmy0 > p:nth-child(n)") %>%
html_text() %>%
list
)}) %>%
unnest(text) -> foo
foo
X2 <- ddply(foo, .(url), summarize,
Xc=paste(text,collapse=","))
final <- merge(temp, X2, by="url")
在这种情况下,我得到了 30 页的文章,但我的脚本只支持一页的抓取。 页面之间唯一变化的是页码 (https://www.sueddeutsche.de/news/**page/1**?search=...)
如果您能告诉我如何一次将所有页面包含到队列中,我将不胜感激。非常感谢:)
【问题讨论】:
-
提示:
lapply,或for循环
标签: r web-scraping rvest