【问题标题】:Web scrape will not return the correct text data网页抓取不会返回正确的文本数据
【发布时间】:2018-09-06 19:04:38
【问题描述】:

我正在尝试从 Second Hand Songs 网站上制作一个简单的表格。我在 R 中使用以下代码和 rvest 包。

library(tidyverse)
library(rvest)

levee_breaks_url <- html('https://secondhandsongs.com/performance/17982')

levee_breaks_url %>% 
  html_node('.field-performer') %>% 
  html_text()

返回

[1] "Performer "

我的目标是获取页面上表格中所有信息的列表。我试过包括html_node('.container'),但结果不正确。

我正在使用selector gadget 并且已经成功地使用它来抓取其他网站,但我已经坚持了一段时间。

【问题讨论】:

    标签: r web-scraping tidyverse rvest


    【解决方案1】:

    试试这个。这将抓取所有表,将它们组合起来,然后为列指定正确的名称。

    library(tidyverse)
    library(rvest)
    
    levee_breaks_url <- read_html('https://secondhandsongs.com/performance/17982')
    
    df <- levee_breaks_url %>% 
      html_nodes('.table') %>% 
      html_table() %>%
      reduce(rbind) %>%
      select(-1) %>%
      rename_all(~levee_breaks_url %>% 
                   html_nodes('th') %>% 
                   html_text() %>% 
                   .[2:5]) %>%
      as.tibble()
    
    df
    #> # A tibble: 32 x 4
    #>    `Title `       `Performer `         `Release date `  Info              
    #>    <chr>          <chr>                <chr>            <chr>             
    #>  1 When the Leve… Kansas Joe and Memp… 1929             First release     
    #>  2 When the Leve… John Campbell        February 20, 19… ""                
    #>  3 When the Leve… Clint Black          2005             ""                
    #>  4 When the Leve… Bennett Harris       August 27, 2008  ""                
    #>  5 When the Leve… Buckwheat Zydeco     2009             ""                
    #>  6 Levee Breaks   Beverley Martyn      April 2014       ""                
    #>  7 When the Leve… Danny B. Harvey - M… October 14, 2014 ""                
    #>  8 When the Leve… Led Zeppelin         November 8, 1971 First releaseSamp…
    #>  9 When the Leve… Judge                1990             ""                
    #> 10 When the Leve… Rosetta Stone        October 14, 1991 Unverified        
    #> # ... with 22 more rows
    

    reprex package (v0.2.0) 于 2018 年 9 月 6 日创建。

    【讨论】:

    • 谢谢。我能问一下你是怎么知道使用'.table'的吗?抓取具有表格格式数据的网站时,这是标准吗?另外,感谢您整理数据!
    • 没问题。不,我不认为它是标准的(虽然我不是专家),我使用了选择器小工具并点击了周围,直到我得到了一些工作。这就是我典型的网络抓取攻击策略。
    猜你喜欢
    • 2021-10-25
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多