【发布时间】:2020-11-16 09:13:12
【问题描述】:
所以我的一个朋友在一个美食博客上写了 800 多篇文章,我希望将所有这些文章提取为 PDF,以便我可以将它们很好地装订并送给他。手动使用 Chrome 的“另存为 PDF”的文章太多了,所以我正在寻找最清晰的方法来运行以这种格式保存网站的循环。我有一个可行的解决方案,但是,最终的 PDF 在每一页上都有丑陋的广告和 cookie 警告横幅。当我在 Chrome 上手动选择“打印”为 PDF 时,我没有看到这一点。有没有办法使用 pagedown 将设置传递给 Chromium 以使其在没有这些元素的情况下打印?我在下面粘贴了我的代码,以及有问题的网站。
library(rvest)
library(dplyr)
library(tidyr)
library(stringr)
library(purrr)
library(downloader)
#Specifying the url for desired website to be scraped
url1 <- paste0('https://www.foodrepublic.com/author/george-embiricos/page/', '1', '/')
#Reading the HTML code from the website
webpage1 <- read_html(url1)
# Pull the links for all articles on George's initial author page
dat <- html_attr(html_nodes(webpage1, 'a'), "href") %>%
as_tibble() %>%
filter(str_detect(value, "([0-9]{4})")) %>%
unique() %>%
rename(link=value)
# Pull the links for all articles on George's 2nd-89th author page
for (i in 2:89) {
url <- paste0('https://www.foodrepublic.com/author/george-embiricos/page/', i, '/')
#Reading the HTML code from the website
webpage <- read_html(url)
links <- html_attr(html_nodes(webpage, 'a'), "href") %>%
as_tibble() %>%
filter(str_detect(value, "([0-9]{4})")) %>%
unique() %>%
rename(link=value)
dat <- bind_rows(dat, links) %>%
unique()
}
dat <- dat %>%
arrange(link)
# form 1-link vector to test with
tocollect<- dat$link[1]
pagedown::chrome_print(input=tocollect,
wait=20,
format = "pdf",
verbose = 0,
timeout=300)
【问题讨论】: