【问题标题】:Question about using rvest and purrr for scraping multiple pages with nested links关于使用 rvest 和 purrr 使用嵌套链接抓取多个页面的问题
【发布时间】:2021-11-27 01:25:15
【问题描述】:

我编写了下面的代码来提取 FLOTUS 在link 上发表的所有演讲。代码如下:

library(rvest)
library(purrr)

url_base <- "https://www.presidency.ucsb.edu/documents/presidential-documents-archive-guidebook/remarks-and-statements-the-first-lady-laura-bush?page=%d"

map_df(1:17, function(i) {

  # simple but effective progress indicator
  cat(".")

  pg <- read_html(sprintf(url_base, i))

  data.frame(name=html_text(html_nodes(pg, ".views-field-title-1.nowrap")),
             title=html_text(html_nodes(pg, "td.views-field-title")),
             year=html_text(html_nodes(pg, ".date-display-single")),
             stringsAsFactors=FALSE)

}) -> flotus

我也想用这段代码来提取相应演讲的文本。有谁知道如何使用我已经编写的代码来做到这一点?如果是这样,那会是什么样子?

【问题讨论】:

    标签: r web-scraping purrr rvest


    【解决方案1】:

    需要使用html_attr() 函数从表的标题列中检索“href”属性链接。

    library(rvest)
    library(purrr)
    
    url_base <- "https://www.presidency.ucsb.edu/documents/presidential-documents-archive-guidebook/remarks-and-statements-the-first-lady-laura-bush?page="
    
    flotus <-map_df(1:16, function(i) {
       
       # simple but effective progress indicator
       cat(".")
       
       pg <- read_html(paste0(url_base, i))
       
       #parse the table
       df <- html_node(pg, "table") %>% html_table()
       
       #obtain the href from the table's Title column
       df$links <-html_nodes(pg, "td.views-field-title") %>% 
                             html_node("a") %>% html_attr("href")
       df
    }) 
    

    以上代码会将语音链接添加为数据框中的附加列。

    第二部分 要提取演讲文本,检索链接列表,然后循环遍历列表,打开页面,提取所需信息,存储并重复。

    #limited the number of pages request for debugging
    map_df(flotus$links[1:3], function(link){
       print(link)
       #Read page
       page <- read_html(link)
       #extract the content and other info
       content <- page %>% html_node("div.field-docs-content") %>% html_text() %>% trimws()
       person <- page %>% html_node("div.field-docs-person") %>% html_text() %>% trimws()
       citation <- page %>% html_node("div.field-prez-document-citation") %>% html_text() %>% trimws()
       
       #add it to a data struture
       data.frame(content, person, citation)
       
       Sys.sleep(1) #Be polite - add a pause to prevent the appearance of attacking the server
    })
    

    这里所有的数据都存储在一个数据框中。然后可以根据未来的意图将该数据框与上方的数据框连接起来。

    【讨论】:

    • 这是应该在我编写的函数之外完成的吗?另外,运行您编写的代码时出现此错误:Error in UseMethod("html_table") : no applicable method for 'html_table' applied to an object of class "xml_missing"
    • 该错误是尝试解析第17页引起的。出于某种原因,rvest 没有检测到该页面上的表格。您的代码在第 17 页上也不起作用。目前原因尚不明显。如果你限制在 1 到 16 个,即 802 个演讲中的 800 个,现在是一个好的开始:(
    • 我有所有我感兴趣的演讲(梅拉尼娅特朗普的演讲)来自我最初发布的代码。你能告诉我如何简单地将个人演讲的文本添加到该代码中吗?
    • 好的,这里是提取链接的代码:data.frame(name=html_text(html_nodes(pg, ".views-field-title-1.nowrap")), title=html_text(html_nodes(pg, "td.views-field-title")), year=html_text(html_nodes(pg, ".date-display-single")), html_nodes(pg, "td.views-field-title") %&gt;% html_node("a") %&gt;% html_attr("href"), stringsAsFactors=FALSE)。但是,我应该怎么做才能提取文本?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    相关资源
    最近更新 更多