【发布时间】:2023-02-01 09:03:40
【问题描述】:
我正在尝试抓取世界卫生组织网站 (https://www.who.int/publications/m) >> 使用“世卫组织文件类型“下拉”新闻发布会文字记录”。
过去我能够使用以下脚本将所有指定的文件类型下载到工作目录,但是我无法正确处理下拉列表。
# Working example
library(tidyverse)
library(rvest)
library(stringr)
page <- read_html("https://www.github.com/rstudio/cheatsheets")
raw_list <- page %>% # takes the page above for which we've read the html
html_nodes("a") %>% # find all links in the page
html_attr("href") %>% # get the url for these links
str_subset("\\.pdf") %>% # find those that end in pdf only
str_c("https://www.github.com", .) %>% # prepend the website to the url
map(read_html) %>% # take previously generated list of urls and read them
map(html_node, "#raw-url") %>% # parse out the 'raw' url - the link for the download button
map(html_attr, "href") %>% # return the set of raw urls for the download buttons
str_c("https://www.github.com", .) %>% # prepend the website again to get a full url
walk2(., basename(.), download.file, mode = "wb") # use purrr to download the pdf associated with each url to the current working directory
如果我从下面开始。我需要包括哪些步骤来说明“世卫组织文件类型“下拉”新闻发布会文字记录" 和 DL 所有文件到工作目录?
library(tidyverse)
library(rvest)
library(stringr)
page <- read_html("https://www.who.int/publications/m")
raw_list <- page %>% # takes the page above for which we've read the html
html_nodes("a") %>% # find all links in the page
html_attr("href") %>% # get the url for these links
str_subset("\\.pdf") %>% # find those that end in pdf only
str_c("https://www.who.int", .) %>% # prepend the website to the url
map(read_html) %>% # take previously generated list of urls and read them
map(html_node, "#raw-url") %>% # parse out the 'raw' url - the link for the download button
map(html_attr, "href") %>% # return the set of raw urls for the download buttons
str_c("https://www.who.int", .) %>% # prepend the website again to get a full url
walk2(., basename(.), download.file, mode = "wb") # use purrr to download the pdf associated with each url to the current working directory
目前,我得到以下信息:
Error in .f(.x\[\[1L\]\], .y\[\[1L\]\], ...) : cannot open URL 'NA'
library(tidyverse)
library(rvest)
library(stringr)
page <- read_html("https://www.who.int/publications/m")
raw_list <- page %>% # takes the page above for which we've read the html
html_nodes("a") %>% # find all links in the page
html_attr("href") %>% # get the url for these links
str_subset("\\.pdf") %>% # find those that end in pdf only
str_c("https://www.who.int", .) %>% # prepend the website to the url
map(read_html) %>% # take previously generated list of urls and read them
map(html_node, "#raw-url") %>% # parse out the 'raw' url - the link for the download button
map(html_attr, "href") %>% # return the set of raw urls for the download buttons
str_c("https://www.who.int", .) %>% # prepend the website again to get a full url
walk2(., basename(.), download.file, mode = "wb") # use purrr to download the pdf associated with each url to the current working directory
结果 下载到工作目录的 PDF
【问题讨论】:
标签: r web-scraping rvest