Versuchen Sie, Google Trends-Daten herunterzuladen, aber der Datumsparameter wird ignoriert?

Ich versuche, Google Trends-Daten im CSV-Format herunterzuladen. Bei grundlegenden Fragen war ich erfolgreich (nach aBlogeintrag von Christoph Riedl).

Problem: Standardmäßig werden Trends ab Januar 2004 zurückgegeben. Ich würde es vorziehen, Trends ab Januar 2011 zurückzugeben. Wenn ich der URL-Anforderung jedoch einen Datumsparameter hinzufüge, wird dieser vollständig ignoriert. Ich bin nicht sicher, wie ich das überwinden soll.

Der folgende Code reproduziert das Problem.

# 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)
}

Melden Sie sich in Ihrem Browser bei Google an (z. B. bei Google Mail). Das in R läuft folgendermaßen ab:

# 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)

Das Folgende gibt erfolgreich Google Trends-Daten seit Januar 2004 zurück (d. H. Kein Datumsparameter).

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

Das Hinzufügen eines Datumsparameters zum Zurückgeben von Trends ab Januar 2013 wird jedoch ignoriert

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

HINWEIS 1: Dasselbe passiert mit dem Parameter cat = category. Das obige ist nur einfacher mit Datum zu zeigen.

HINWEIS 2: Da Google die Daten abhängig vom Startdatum neu skaliert, handelt es sich nicht einfach um das Filtern des data.frame. Ich bin daran interessiert, warum der Datumsparameter ignoriert wird.

Vielen Dank für Ihre Zeit.

Antworten auf die Frage(2)

Ihre Antwort auf die Frage