【问题标题】:R code for downloading all the pdfs given on a site: Web scraping用于下载网站上所有 pdf 的 R 代码:Web 抓取
【发布时间】:2021-10-27 06:41:35
【问题描述】:

我想在 R 中编码,它可以下载此 URL 上给出的所有 pdf:https://www.rbi.org.in/scripts/AnnualPublications.aspx?head=Handbook%20of%20Statistics%20on%20Indian%20Economy 然后下载文件夹中的所有pdf。我在https://towardsdatascience.com 的帮助下尝试了以下代码,但代码错误为

library(tidyverse)
library(rvest)
library(stringr)
library(purrr)
page <- read_html("https://www.rbi.org.in/scripts/AnnualPublications.aspx? 
head=Handbook%20of%20Statistics%20on%20Indian%20Economy") %>%

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://rbi.org.in", .) %>% # 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.rbi.org.in", .) %>% # prepend the website again to get a full url
for (url in raw_list)
{ download.file(url, destfile = basename(url), mode = "wb") 
} 

我无法解释为什么代码会出错。如果有人可以帮助我。

【问题讨论】:

    标签: html r json url web-scraping


    【解决方案1】:

    有一些小错误。本网站使用大写字母作为 PDF 结尾,您无需使用str_c("https://rbi.org.in", .)。最后,我认为使用 purrr 的 walk2 函数更流畅(可能是在原始代码中)。

    我还没有执行代码,因为我不需要那么多pdf,所以,如果它有效,请报告。

    library(tidyverse)
    library(rvest)
    library(stringr)
    library(purrr)
    page <- read_html("https://www.rbi.org.in/scripts/AnnualPublications.aspx?head=Handbook%20of%20Statistics%20on%20Indian%20Economy")
      
      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") %>% 
      walk2(., basename(.), download.file, mode = "wb")
    

    【讨论】:

    • 嗨。谢谢。代码运行良好。我还有一个疑问。如果我希望将 pdf 命名为 pdf1、pdf2 等而不是默认名称,我该如何更改代码
    【解决方案2】:

    在尝试运行您的代码时,我遇到了“验证您是人类”和“请确保您的浏览器启用了 Javascript”对话框。这表明您无法使用 Rvest 打开页面,但您需要改用 RSelenium browser automation

    这是一个使用 RSelenium 的修改版本

    library(tidyverse)
    library(stringr)
    library(purrr)
    library(rvest)
    
    library(RSelenium)
    
    rD <- rsDriver(browser="firefox", port=4545L, verbose=F)
    remDr <- rD[["client"]]
    
    remDr$navigate("https://www.rbi.org.in/scripts/AnnualPublications.aspx?head=Handbook%20of%20Statistics%20on%20Indian%20Economy")
    page <- remDr$getPageSource()[[1]]
    read_html(page) -> html
    
    html %>%
    html_nodes("a") %>%  
    html_attr("href") %>% 
    str_subset("\\.PDF") -> urls
    urls %>% str_split(.,'/') %>% unlist() %>% str_subset("\\.PDF") -> filenames
    
    for(u in 1:length(urls)) {
     cat(paste('downloading: ', u, ' of ', length(urls) '\n'))
     download.file(urls[u], filenames[u], mode='wb')
     Sys.sleep(1)
    }
    

    【讨论】:

    • Re: 文件名 -- 你将 filenames 更改为例如paste0('pdf',seq(1, length(urls),1),'.pdf') -&gt; filenames
    • 嗨。非常感谢:)
    • 嗨。我还有一个问题。因此,在右侧列中,您可以看到各个年份的数据,例如:2020、2019 等。我们能否以某种方式修改代码,以便该代码也可以下载这些年份的数据。
    • @Manu - 我相信你可以使用 elem&lt;- remDr$findElement(using="link text", "2020)elem$clickElement() 但我还没有测试过这个解决方案。我建议提出一个新问题。
    • 嗨。谢谢。 2020 年的网页正在打开(如在 2020 年访问),但不知何故,文件未在系统中下载。是的,这将作为一个新问题提出。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多