r - 模拟浏览器访问 Google 趋势数据

标签 r

我正在尝试从 R 中的 Google Trends 下载数据。方法 here有效,但在达到配额限制之前仅有效几次(而且我什至没有运行批处理作业)。

与此同时,我仍然可以在浏览器中获取 Google 趋势的输出。因此,我的兴趣是:R 有没有一种方法可以模拟浏览器,以便我可以按照浏览器(Chrome,如果重要的话)访问 R 中的数据?

最佳答案

您可以使用浏览器使用 browseURL() 打开 R 中的链接。使用下面的函数创建链接。

URL_GT=function(keyword="", country=NA, region=NA, year=NA, month=1, length=3){
# keyword can contain up to five words
# country is the 2 letter country code
# region is also a 2 letter code
# year: if you want a specific year, put it here
# month: starting month if you have specified the year
# length: the number of months you want if you have specified the year

  start="http://www.google.com/trends/trendsReport?hl=en-US&q="
  end="&cmpt=q&content=1&export=1"
  geo=""
  date=""

#Geographic restrictions
  if(!is.na(country)) {
    geo="&geo="
    geo=paste(geo, country, sep="")
    if(!is.na(region)) geo=paste(geo, "-", region, sep="")
  }

  queries=keyword[1]
  if(length(keyword)>1) {
    for(i in 2:length(keyword)){
    queries=paste(queries, "%2C ", keyword[i], sep="")
    }
  }

#Dates
  if(!is.na(year)){
    date="&date="
    date=paste(date, month, "%2F", year, "%20", length, "m", sep="")
  }

  URL=paste(start, queries, geo, date, end, sep="")
  URL <- gsub(" ", "%20", URL)
  return(URL)
}

url <- URL_GT('stackoverflow')
browseURL(url)

如果您想下载大量文件,请查看these helper functions .

关于r - 模拟浏览器访问 Google 趋势数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30421624/

相关文章:

r - R 中使用 highcharter 绘制 3D 散点图

r - 计算加权 Logit 模型的边际效应

r - 动态扩展引用类时如何控制继承

python - 如何对包含 R 函数的 pyspark RDD 进行分区

r - "Error in continuous_scale"和 "error in inherits"ggplot2 & R 2.14.2

r - R中的VarCorr重载,lme4和nlme都有版本

r - 在 R 中加载/读取数据占用太多内存

r - 滑动 (io2012) : how to change the slide size according to screen resolution?

r - 如何在R中的Cox回归模型中预测生存时间?

r - 由数据框中的列名引起的 arima xreg 参数错误