我正在尝试从 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/