【问题标题】:Scrape multiple URLs with rvest使用 rvest 抓取多个 URL
【发布时间】:2020-06-08 15:07:06
【问题描述】:

rvest 中使用read_html 时如何抓取多个网址?目标是从各个 url 中获取由文本主体组成的单个文档,以在其上运行各种分析。

我尝试连接网址:

 url <- c("https://www.vox.com/","https://www.cnn.com/")
   page <-read_html(url)
   page
   story <- page %>%
        html_nodes("p") %>%  
        html_text

read_html之后得到错误:

 Error in doc_parse_file(con, encoding = encoding, as_html = as_html, options = options) : 
 Expecting a single string value: [type=character; extent=3].

这并不奇怪,因为read_html 一次可能只处理一条路径。但是,我可以使用不同的函数或转换来同时抓取多个页面吗?

【问题讨论】:

    标签: html r screen-scraping rvest


    【解决方案1】:

    您可以使用map(或在基础R 中:lapply)循环遍历每个url 元素;这是一个例子

    url <- c("https://www.vox.com/", "https://www.bbc.com/")
    page <-map(url, ~read_html(.x) %>% html_nodes("p") %>% html_text())
    str(page)
    #List of 2
    # $ : chr [1:22] "But he was acquitted on the two most serious charges he faced." "Health experts say it’s time to prepare for worldwide spread on all continents." "Wall Street is waking up to the threat of coronavirus as fears about the disease and its potential global econo"| __truncated__ "Johnson, who died Monday at age 101, did groundbreaking work in helping return astronauts safely to Earth." ...
    # $ : chr [1:19] "" "\n                                                            The ex-movie mogul is handcuffed and led from cou"| __truncated__ "" "27°C" ...
    

    返回对象是list

    附言。我更改了第二个url 元素,因为"https://www.cnn.com/"html_nodes("p") %&gt;% html_text() 返回了NULL

    【讨论】:

    • 感谢@Maurits Evers - 这非常有帮助!运行“Purrr”后,地图功能起作用。但我也有运行某些 URL 的问题。在各种 URL 上运行“html_nodes”时,我得到:字符 (0)。我想知道我是否有选择器问题。我想知道如何设置 html_nodes 以捕获所有选择器类并返回文本?
    • @AnneBoysen 这似乎是一个不同的问题,您可能想在新帖子中提出。我建议通过在答案旁边设置绿色复选标记来关闭此问题;然后打开一个带有明确问题陈述的新问题。很高兴看看。
    猜你喜欢
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2022-01-03
    相关资源
    最近更新 更多