【问题标题】:html() not reading urls from list correctlyhtml() 没有从列表中正确读取 url
【发布时间】:2019-04-27 17:10:29
【问题描述】:

我想从网站上抓取 pdf。我可以手动下载它们,但这是一个学习练习,以便我以后可以做更多的网站。

我正在尝试使用 rvest,但基本功能已关闭。

library(tidyverse)
library(rvest)

trial <- 'http://www.fairlabor.org/report/2015-annual-public-report'
page2<- html(trial)
page2 %>% html_nodes("a") %>% html_attr("href") %>% str_subset(".pdf") %>% download.file("~/downloads/file.pdf")

我想对所有的年度报告都这样做。所以我试过了:

url <- 'http://www.fairlabor.org/impact/reports'
page<- html(url)
links_init <- page %>% html_nodes("a") %>% html_attr("href")
links <- links_init[seq(from = 53, to = 72, by = 2)]
new_urls <- paste0(url, links)

# trying to repeat the above basic example. If it succeeds, I can put in a for loop.
test <- html(new_urls[2])
test %>% html_nodes("a") %>% html_attr("href") %>% str_subset(".pdf")

相反,我得到一个空结果,因为它似乎正在抓取不同的页面。我不知道为什么会这样,因为page2test 似乎是同一个字符串。

任何人都知道这里发生了什么以及我该如何纠正它?

我尝试使用as.character(quote()) 明确地将我的网址向量new_urls 转换为字符串。

【问题讨论】:

    标签: r rvest


    【解决方案1】:

    查看基本 URL,似乎有 2003 年到 2017 年的报告。我们可以先创建一个 urls 的列表

    urls <- paste0("http://www.fairlabor.org/report/",seq(2003, 2017),
                   "-annual-public-report")
    
    urls
    # [1] "http://www.fairlabor.org/report/2003-annual-public-report"
    # [2] "http://www.fairlabor.org/report/2004-annual-public-report"
    # [3] "http://www.fairlabor.org/report/2005-annual-public-report"
    # [4] "http://www.fairlabor.org/report/2006-annual-public-report"
    # [5] "http://www.fairlabor.org/report/2007-annual-public-report"
    # [6] "http://www.fairlabor.org/report/2008-annual-public-report"
    # [7] "http://www.fairlabor.org/report/2009-annual-public-report"
    # [8] "http://www.fairlabor.org/report/2010-annual-public-report"
    # [9] "http://www.fairlabor.org/report/2011-annual-public-report"
    #[10] "http://www.fairlabor.org/report/2012-annual-public-report"
    #[11] "http://www.fairlabor.org/report/2013-annual-public-report"
    #[12] "http://www.fairlabor.org/report/2014-annual-public-report"
    #[13] "http://www.fairlabor.org/report/2015-annual-public-report"
    #[14] "http://www.fairlabor.org/report/2016-annual-public-report"
    #[15] "http://www.fairlabor.org/report/2017-annual-public-report"
    

    现在,每个 URL 的报告都不存在,这可能会导致错误,因此我们可以使用 tryCatch 跳过下载这些报告。我们可以使用lapply从所有urls下载报告

    library(tidyverse)
    library(rvest)
    
    lapply(urls, function(x) tryCatch({ x %>%
      read_html() %>%
      html_nodes("a") %>%
      html_attr("href") %>%
      str_subset(".pdf") %>%
      download.file(paste0("~/downloads/", basename(x), ".pdf"))}, 
      error = function(e) { }))
    

    这会将 pdf 报告下载到名为 2003-annual-public-report.pdf2006-annual-public-report.pdf 等的下载文件夹中。

    【讨论】:

    • 谢谢!这段代码在 2004 年和 2005 年的报告中遗漏了,所以我在 download.file 周围添加了一个 mapmap(., download.file(., paste0("~/downloads/", basename(x)))}
    猜你喜欢
    • 1970-01-01
    • 2021-02-16
    • 2020-02-17
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多