【问题标题】:Trying to download Google Trends data but date parameter is ignored?尝试下载 Google 趋势数据但忽略日期参数?
【发布时间】:2014-03-05 21:46:22
【问题描述】:

我正在尝试以 csv 格式下载 Google 趋势数据。对于基本查询,我已经成功了(在 Christoph Riedl 的 blog post 之后)。

问题:默认情况下,从 2004 年 1 月开始返回趋势。我希望它从 2011 年 1 月开始返回趋势。但是,当我向 url 请求添加日期参数时,它会被完全忽略。我不确定如何克服这个问题。

以下是重现问题的代码。

# Just copy/paste this stuff - these are helper functions
require(RCurl)

# This gets the GALX cookie which we need to pass back with the login form
getGALX <- function(curl) {
  txt = basicTextGatherer()
  curlPerform( url=loginURL, curl=curl, writefunction=txt$update, header=TRUE, ssl.verifypeer=FALSE )

  tmp <- txt$value()

  val <- grep("Cookie: GALX", strsplit(tmp, "\n")[[1]], val = TRUE)
  strsplit(val, "[:=;]")[[1]][3]

  return( strsplit( val, "[:=;]")[[1]][3]) 
}

# Function to perform Google login and get cookies ready
gLogin <- function(username, password) {
  ch <- getCurlHandle()

  ans <- (curlSetOpt(curl = ch,
                     ssl.verifypeer = FALSE,
                     useragent = getOption('HTTPUserAgent', "R"),
                     timeout = 60,         
                     followlocation = TRUE,
                     cookiejar = "./cookies",
                     cookiefile = ""))

  galx <- getGALX(ch)
  authenticatePage <- postForm(authenticateURL, .params=list(Email=username, Passwd=password, GALX=galx, PersistentCookie="yes", continue="http://www.google.com/trends"), curl=ch)

  authenticatePage2 <- getURL("http://www.google.com", curl=ch)

  if(getCurlInfo(ch)$response.code == 200) {
    print("Google login successful!")
  } else {
    print("Google login failed!")
  }
  return(ch)
}

# returns string w/o leading or trailing whitespace
trim <- function (x) gsub("^\\s+|\\s+$", "", x)

get_interest_over_time <- function(res, clean.col.names = TRUE) {
  # remove all text before "Interest over time" data block begins
  data <- gsub(".*Interest over time", "", res)

  # remove all text after "Interest over time" data block ends
  data <- gsub("\n\n.*", "", data)

  # convert "interest over time" data block into data.frame
  data.df <- read.table(text = data, sep =",", header=TRUE)

  # Split data range into to only end of week date 
  data.df$Week <- gsub(".*\\s-\\s", "", data.df$Week)
  data.df$Week <- as.Date(data.df$Week)

  # clean column names
  if(clean.col.names == TRUE) colnames(data.df) <- gsub("\\.\\..*", "", colnames(data.df))

  # return "interest over time" data.frame
  return(data.df)
}

请在您的浏览器中登录 Google(例如登录 gmail)。在 R 中运行以下内容:

# Username and password
username <- "email@address"
password <- "password"

# Login and Authentication URLs
loginURL     <- "https://accounts.google.com/accounts/ServiceLogin"
authenticateURL <- "https://accounts.google.com/accounts/ServiceLoginAuth"
trendsURL       <- "http://www.google.com/trends/TrendsRepport?"

# Google authentication
ch <- gLogin( username, password )
authenticatePage2 <- getURL("http://www.google.com", curl=ch)

以下成功返回 2004 年 1 月以来的 google 趋势数据(即无日期参数)

res <- getForm(trendsURL, q="ggplot2, ggplot", content=1, export=1, graph="all_csv", curl=ch)
df <- get_interest_over_time(res)
head(df)

        Week ggplot2 ggplot
1 2004-01-10       0      0
2 2004-01-17       0      0
3 2004-01-24       0      0
4 2004-01-31       0      0
5 2004-02-07       0      0
6 2004-02-14       0      0

但是,添加日期参数以返回从 2013 年 1 月开始的趋势将被忽略

res <- getForm(trendsURL, q="ggplot2, ggplot", date = "1/2013 11m", content=1, export=1, graph="all_csv", curl=ch)
df <- get_interest_over_time(res)
head(df)

        Week ggplot2 ggplot
1 2004-01-10       0      0
2 2004-01-17       0      0
3 2004-01-24       0      0
4 2004-01-31       0      0
5 2004-02-07       0      0
6 2004-02-14       0      0

注意 1:cat=category 参数也会发生同样的情况。以上只是更容易显示日期。

注意 2:由于 Google 会根据开始日期重新调整数据,因此这不是简单地过滤 data.frame 的情况。我对为什么忽略 date 参数感兴趣。

感谢您的宝贵时间。

【问题讨论】:

    标签: r rcurl


    【解决方案1】:

    如果你只写一年就可以了:

    res <- getForm(trendsURL, q="ggplot2, ggplot", date = "2013", content=1, export=1, graph="all_csv", curl=ch)
    

    但我不知道如何在日期中添加月份和日期。 可能是因为在 GoogleTrends 网页上,您可以从列表中选择时间范围:

    “过去 7 天”、“过去 30 天”、...、“2013”​​、“2012”、...

    但如果我尝试date="Past 90 days",它仍然不起作用。

    【讨论】:

      【解决方案2】:

      我已成功使用日期规范date="2011-1"(2011 年 1 月)获取月度数据。我查看了页面背后的来源 - 也许你可以在那里找到答案。

      如果您弄清楚日期说明,请再次发布。

      【讨论】:

      • @Tony Breyal:你有没有成功指定日期间隔?
      • @Tonybreyal:你有关于日期间隔规范的更新吗?
      猜你喜欢
      • 2012-01-27
      • 1970-01-01
      • 2017-12-13
      • 2013-11-18
      • 2022-10-07
      • 1970-01-01
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多