【问题标题】:RVest scraping of data and image titleRVest 抓取数据和图片标题
【发布时间】:2020-08-06 02:05:11
【问题描述】:

我正在尝试同时抓取数据和图像的标题。

library(rvest) 

    test <- paste0("https://247sports.com/Season/2021-Football/Commits/?Page=", 1:38)
    
    test_data <- map_df(commits, ~.x %>% read_html %>%
                         html_nodes(".ri-page__star-and-score .score , .position , .ri-page__name-link") %>%
                         html_text() %>% 
                         str_trim %>% 
                         str_split("   ") %>% 
                         matrix(ncol = 3, byrow = T) %>% 
                         as.data.frame)

这为我提供了我需要的四个数据字段。但我也想获得图片标题,它位于:

<img alt="South Carolina" class="jsonly" src="https://s3media.247sports.com/Uploads/Assets/627/649/4649627.png?fit=bounds&amp;crop=50:50,offset-y0.50&amp;width=50&amp;height=50" title="South Carolina" style="opacity: 1;">

所以我还想提取 alt 或 title,两者都给了我名字(在这个例子中,它是 South Carolina)。我知道如何使用httr_attr 仅将其提取为标题,但我不知道如何将它们全部组合在一起,因此总共有五个字段。

【问题讨论】:

    标签: r rvest


    【解决方案1】:

    您可以通过以下方式获取标题:

    library(rvest)
    library(tidyverse)
    
    test <- 'https://247sports.com/Season/2021-Football/Commits/?Page=1'
    
    test_data$title <- test %>%
                        read_html %>%
                        html_nodes('div.status img') %>%
                        html_attr('title') %>%
                       .[c(TRUE, FALSE)]
    
    test_data
    #                    V1                  V2     V3   V4              title
    #1           Kyle Ecker Committed: 8/5/2020     NA   OT          San Diego
    #2     Josh Bertholotte Committed: 8/5/2020     NA  OLB             Hawaii
    #3        Antario Brown Committed: 8/5/2020 0.8516   RB     South Carolina
    #4       J'Kalon Carter Committed: 8/5/2020     NA   WR     Illinois State
    #5       Stephon Dubose Committed: 8/5/2020     NA   OG       Old Dominion
    #...
    

    我使用.[c(TRUE, FALSE)] 选择备用值,因为使用html_attr('title') 会返回每个值两次。


    要将其集成到map 函数中,我们可以这样做:

    all_data <- map_df(commits, ~{
                 webpage <- .x %>% read_html
                 df1 <-  webpage %>%
                        html_nodes(".ri-page__star-and-score .score ,.position ,.ri-page__name-link") %>%
                        html_text() %>% 
                        str_trim %>% 
                        str_split("   ") %>% 
                        matrix(ncol = 3, byrow = T) %>% 
                        as.data.frame
                 df1$title <- webpage %>%
                                html_nodes('div.status img') %>%
                                html_attr('title') %>%
                               .[c(TRUE, FALSE)]
                 df1
    })
    

    【讨论】:

    • 谢谢,但这是否需要我从上面运行我的脚本和你的单独运行?这最终是 map_df 函数的一部分,所以我想一次性运行它。
    • 是的,您需要单独执行此操作,因为其他 4 个值在文本中,并且该值在属性之一中。如果您无法将其集成到其中,您可以编辑您的帖子以包含 map_df 代码。
    • 你能检查一下更新后的答案是否有帮助吗?
    • 错误$&lt;-.data.frame(*tmp*, "title", value = c("California", "Syracuse", : 替换有49行,数据有50行
    • 哪个页面返回该错误?你能去那个页面看看是否所有的Status 值都存在吗?
    猜你喜欢
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 2015-08-23
    • 1970-01-01
    相关资源
    最近更新 更多