微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过R下载Google趋势数据

如何解决通过R下载Google趋势数据

我正在使用此脚本从Google趋势下载数据。但是,它不会在最近三天内打印。换句话说,我直到2020年9月28日才得到结果,现在是01/10/2020。

是否可以下载更多最新数据?

谢谢。

注意:该脚本是从here中检索的。

 library(gtrendsr)
    library(tidyverse)
    library(lubridate)

get_daily_gtrend <- function(keyword = 'Taylor Swift',geo = 'UA',from = '2013-01-01',to = '2019-08-15') {
  if (ymd(to) >= floor_date(Sys.Date(),'month')) {
    to <- floor_date(ymd(to),'month') - days(1)
    
    if (to < from) {
      stop("Specifying \'to\' date in the current month is not allowed")
    }
  }

  mult_m <- gtrends(keyword = keyword,geo = geo,time = paste(from,to))$interest_over_time %>%
    group_by(month = floor_date(date,'month')) %>%
    summarise(hits = sum(hits)) %>%
    mutate(ym = format(month,'%Y-%m'),mult = hits / max(hits)) %>%
    select(month,ym,mult) %>%
    as_tibble()
  
  pm <- tibble(s = seq(ymd(from),ymd(to),by = 'month'),e = seq(ymd(from),by = 'month') + months(1) - days(1))
  
  raw_trends_m <- tibble()
  
  for (i in seq(1,nrow(pm),1)) {
    curr <- gtrends(keyword,time = paste(pm$s[i],pm$e[i]))
    print(paste('for',pm$s[i],pm$e[i],'retrieved',count(curr$interest_over_time),'days of data'))
    raw_trends_m<- rbind(raw_trends_m,curr$interest_over_time)
  }
  
  trend_m <- raw_trends_m %>%
    select(date,hits) %>%
    mutate(ym = format(date,'%Y-%m')) %>%
    as_tibble()
  
  trend_res <- trend_m %>%
    left_join(mult_m,by = 'ym') %>%
    mutate(est_hits = hits * mult) %>%
    select(date,est_hits) %>%
    as_tibble() %>%
    mutate(date = as.Date(date))
  
  return(trend_res)
}

get_daily_gtrend(keyword = 'Taylor Swift',to = '2019-08-15')

解决方法

这是Google趋势数据的工作方式。即使您访问该网站并下载过去7天到最近90天之内的任何数据,它也可以为您提供三天前的每日数据。所以这是设计使然。 我不确定gTrendsR是否每小时检索一次数据,但是您可以从网站上手动检索最近7天的数据,以获取请求前几个小时的每小时数据,也可以使用PyTrends软件包,该软件包可以每小时返回一次日期。如果您掌握了每小时的数据,则可以轻松地将其汇总到每天。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。