【问题标题】:rvest scrape links from webpagervest 从网页中抓取链接
【发布时间】:2021-04-29 06:52:59
【问题描述】:

我正在使用rvest 从杂志“The Hustle”中抓取一些链接。我用过这段代码

library(rvest)

page <- read_html("https://thehustle.co/daily/page/33/") %>% 
  html_nodes(".daily-article-title") %>% 
  html_attr('href')

但是,这会返回一个包含 30 个 NA 的向量。我使用 SelectorGadget 来查找课程,所以不确定这里出了什么问题。

【问题讨论】:

    标签: r rvest


    【解决方案1】:

    链接位于类'.daily-article-title' 上方。这是一种获取标题和相应链接的方法。

    library(rvest)
    
    webpage <- read_html("https://thehustle.co/daily/page/33/")
    
    webpage %>%
      html_nodes("h3.daily-article-title") %>% 
      html_text() -> title
    
    title
    
    # [1] "\nApple buys itself a $400m Christmas present\n"          
    # [2] "\nSan Francisco wages war on robots\n"                    
    # [3] "\nThe US could lose its greatest export\n"                
    # [4] "\n\"Mom, where do podcasts come from?\"\n"                
    # [5] "\nTencent Music to team up with Spotify?\n"               
    # [6] "\nFirst rule of the Farmers Business Network?\n"          
    # [7] "\nSpiegel goes HAM on social media\n"                     
    # [8] "\nBanks won't take weed companies’ cash\n"                
    # [9] "\nThe Koch bros just took a $650m stake in Time\n"        
    #[10] "\n4 mins to smarter Monday smalltalk\n"     
    #...
    #...
               
    webpage %>%
      html_nodes("[class='col-md-12 daily-wrap clearfix'] a") %>%
      html_attr('href') -> link
    
    # [1] "https://thehustle.co/apple-christmas-present"                          
    # [2] "https://thehustle.co/war-on-robots"                                    
    # [3] "https://thehustle.co/big-data-trade-nafta-daily"                       
    # [4] "https://thehustle.co/apple-podcast-market"                             
    # [5] "https://thehustle.co/tencent-spotify-truce"                            
    # [6] "https://thehustle.co/first-rule-of-farmers"                            
    # [7] "https://thehustle.co/snap-anti-facebook"                               
    # [8] "https://thehustle.co/weed-banking"                                     
    # [9] "https://thehustle.co/pepshi-bros"                                      
    #[10] "https://thehustle.co/rundown"    
    #...
    #...                              
    

    【讨论】:

      猜你喜欢
      • 2020-01-30
      • 1970-01-01
      • 2018-03-20
      • 2023-03-26
      • 1970-01-01
      • 2021-03-31
      • 2019-07-25
      • 2021-12-25
      • 2020-07-18
      相关资源
      最近更新 更多