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

时间:2015-05-24 08:43:46

标签: r

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

与此同时,我仍然可以在浏览器中获得Google趋势的输出。因此我感兴趣:R是否有办法模拟浏览器,这样我就可以在浏览器中访问R中的数据(Chrome,如果重要的话)?

1 个答案:

答案 0 :(得分:4)

您可以使用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