【问题标题】:scraping google news with Rvest使用 Rvest 抓取谷歌新闻
【发布时间】:2019-04-08 14:54:02
【问题描述】:

我曾经有一个功能可以从google news 抓取标题,但似乎他们改变了他们的 CSS 或其他东西。它不再起作用了。

这是我最接近修复它的尝试:

library('rvest')
library('tidyverse')
headlines <- function(){
  message("Here are some of today's headlines:")
  html <- read_html("https://news.google.com/news/?ned=us&gl=US&hl=en")
  headlines = html %>%
    html_nodes(".SbNwzf") %>%
    html_text()

  for(i in 1:10){
    cat(paste("\t",headlines[i],"\n\n"))
    Sys.sleep(1.3)
  }
  headlines

}
headlines()

这与我想要的非常接近,但我希望只获得头条新闻。似乎这也是文章的第一部分。

另外,如果有人有办法只获取每个框的顶部标题,那将是更可取的。正在尝试节点“.VDXfz”,但它返回空白。我正在使用selector gadget

谢谢!

【问题讨论】:

  • 也许this 的回答会有用
  • @Didi 感谢您的建议,但这对我不起作用。然而,它确实给了我一些尝试新事物的动力,我解决了它。谢谢!

标签: r web-scraping rvest


【解决方案1】:

这就是最终的工作:

headlines <- function(){
  message("Here are some of today's headlines:")
  html <- read_html("https://news.google.com/news/?ned=us&gl=US&hl=en")
  headlines = html %>%
    html_nodes('.DY5T1d') %>% # <-- THIS
    html_text()

  for(i in (0:9)*5+1){ # PRINT EVERY 5TH ENTRY
    cat(paste("\t",headlines[i],"\n\n"))
    Sys.sleep(1.3)
  }

}

不幸的是,这取决于谷歌新闻保持 1 篇关于某个主题的主要文章和 4 篇支持文章的格式。我仍然无法确定主要文章的标记,因此我可以单独挑选它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    • 2022-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多