如何使用rvest将谷歌新闻结果拼凑成数据框 [英] How to scrape Google News results into a data.frame with rvest

查看:0
本文介绍了如何使用rvest将谷歌新闻结果拼凑成数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过其他so问题,我发现了如何获取标题,但我不知道Google代码将链接存储在哪里。

我想要标题及其相应链接的2栏数据框架。

library(rvest)
library(tidyverse)


dat <- read_html("https://news.google.com/search?q=coronavirus&hl=en-US&gl=US&ceid=US%3Aen") %>%
  html_nodes('.DY5T1d') %>% #
  html_text()

dat

推荐答案

在大量检查谷歌网页代码后,我找到了我要找的东西。我也看到了这些描述,所以我基本上重新构建了谷歌新闻RSS提要。

library(rvest)
library(tidyverse)


news <- function(term) {
  
  html_dat <- read_html(paste0("https://news.google.com/search?q=",term,"&hl=en-US&gl=US&ceid=US%3Aen"))

  dat <- data.frame(Link = html_dat %>%
                      html_nodes('.VDXfz') %>% 
                      html_attr('href')) %>% 
    mutate(Link = gsub("./articles/","https://news.google.com/articles/",Link))
  
  news_dat <- data.frame(
    Title = html_dat %>%
      html_nodes('.DY5T1d') %>% 
      html_text(),
    Link = dat$Link
  )
  
  return(news_dat)
}

news("coronavirus")

这篇关于如何使用rvest将谷歌新闻结果拼凑成数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆