PMassicotte / gtrendsR

R functions to perform and display Google Trends queries
355 stars 112 forks source link

searches with different time periods #360

Closed alexCardazzi closed 2 years ago

alexCardazzi commented 4 years ago

I am trying to search a term in different places but for different time periods. While I can input a vector of search terms or places, I cannot seem to use two custom ranges. For example, I want to search "NBA" in 2010-NY and 2011-CA. This is possible in the google trends website, but I keep getting errors with the function. Is there a quick fix?

Error Code: Error in interest_over_time(widget, comparison_item, tz) : Status code was not 200. Returned status code:400

Edit: I am using: time = c("2010-01-01 2010-01-31", "2011-01-01 2011-01-31")

PMassicotte commented 4 years ago

Can you make a reproducible example please? So I can copy/paste it.

alexCardazzi commented 4 years ago
library("gtrendsR")

#this breaks
gtrends(keyword = c("NBA", "NBA"),
        geo = c("US-NY", "US-CA"),
        time = c("2010-01-01 2010-01-31", "2011-01-01 2011-01-31")) -> x

#this works
gtrends(keyword = c("NBA", "NBA"),
        geo = c("US-NY", "US-CA"),
        time = c("all", "all")) -> y
PMassicotte commented 4 years ago

Does it work on the Google Trends website?

alexCardazzi commented 4 years ago

Yes. I used a different time period here for demonstration purposes, but both work. Thanks for looking into this.

https://trends.google.com/trends/explore?date=2012-01-01%202012-12-31,2013-01-01%202013-12-31&geo=US-NY,US-CA&q=nba,nba

PMassicotte commented 4 years ago

The problem is here:

https://github.com/PMassicotte/gtrendsR/blob/2a0094fe0305a4b1e5572b7375c9252ab43626c1/R/zzz.R#L197-L220

Need to use multirange and not multiline. Also, locale should be removed from the constructed URL.

agenin commented 4 years ago

@PMassicotte I'm receiving the same error for this query:

DMA501 <- gtrends(keyword = "wolf", geo = c("US-501"), time = "today+5-y", gprop = c("web"), category = 0, hl = "en-US", low_search_volume = TRUE, cookie_url = "http://trends.google.com/Cookies/NID")$interest_over_time

is it resolved by fixing the problem you've cited?

PMassicotte commented 4 years ago

Problem still not fixed.

takakubo commented 2 years ago

I am facing the same error. It is helpful if you could share the measures. Thank you.

bpffjl commented 2 years ago

The same error has just shown up for me too. I've been running this: g <- gtrends(keyword = "food stamps", geo = "US-MN", time = "today 12-m") for over two years almost daily, and it failed today. I tried some simpler examples from the documentation and they are now failing as well. Thoughts?

PMassicotte commented 2 years ago

A new version (1.5.1) has been uploaded to CRAN. This is fixing it.

takakubo commented 2 years ago

Thanks. A new version works!

bpffjl commented 2 years ago

Awesome! Works for me too. Thanks!

jlett48 commented 2 years ago

I just started getting this issue 3 days ago after this has been running for 2 years....any suggestion

gd <- gtrends(keyword = "tires", geo = "US", time = "today+5-y", gprop = c("web", "news", "images", "froogle", "youtube"), category = 438, hl = "en-US", low_search_volume = FALSE, cookie_url = "http://trends.google.com/Cookies/NID", tz = 0, onlyInterest = TRUE)

Error in interest_over_time(widget, comparison_item, tz) : Status code was not 200. Returned status code:401

PMassicotte commented 2 years ago

Works here. Please update package.

library(gtrendsR)

plot(gtrends(
  keyword = "tires", geo = "US", time = "today+5-y",
  gprop = c("web", "news", "images", "froogle", "youtube"),
  category = 438, hl = "en-US", low_search_volume = FALSE,
  cookie_url = "http://trends.google.com/Cookies/NID", tz = 0,
  onlyInterest = TRUE
))

Created on 2022-05-27 by the reprex package (v2.0.1)

rkersey8208 commented 2 years ago

Along these same lines, I'm trying to do a custom date range where the start date is constant, but the end date resolves to the most recent week of data (hoping to run this every Monday and get the previous week). I tried the below, but get the error: "Cannot parse supplied time format."

Anyone have any tips?

today <- Sys.Date() end <- format(today,format="%Y-%m-%d")

searchRes1 <- gtrendsR::gtrends(keyword = search_terms_trainers, time="2019-01-01 end")

eddelbuettel commented 2 years ago

Your code is not right. You assign to a variable end but then in the string refer to a fixed string "end". That is not the same.

Maybe something like ths works:

datestring <- paste("2019-01-01",  format(Sys.Date(), "%Y-%m-%d"))
searchRes1 <- gtrendsR::gtrends(keyword = search_terms_trainers, time=datestring)
rkersey8208 commented 2 years ago

Your code is not right. You assign to a variable end but then in the string refer to a fixed string "end". That is not the same.

Maybe something like ths works:

datestring <- paste("2019-01-01",  format(Sys.Date(), "%Y-%m-%d"))
searchRes1 <- gtrendsR::gtrends(keyword = search_terms_trainers, time=datestring)

Thank you. Looks like that will work. Much appreciated

anhnguyendepocen commented 1 year ago

library(gtrendsR) library(tidyverse) trends <- gtrends(keyword = c("python", "R")) Error in interest_over_time(widget, comparison_item, tz) : Status code was not 200. Returned status code:429

Kacimi-Amine commented 1 year ago

Same error here ! I'm using v ‘1.5.1’ ... any solution ?

DataSocialist commented 1 year ago

I have been getting the same error when trying to gather data for multiple time frames. It works fine when only pulling data for a single time frame. I am using 1.5.1

Error in interest_over_time(widget, comparison_item, tz) : Status code was not 200. Returned status code:401

Any solutions?

PMassicotte commented 1 year ago

Make a reproducible example in R and also link the result you are getting directly on Google Trends website.

DataSocialist commented 1 year ago

https://trends.google.com/trends/explore?date=2019-01-01%202019-10-20&geo=CH&q=svp&hl=en-US

list_keywords <- c("svp", "fdp", "cvp",  "sp",  "glp")

data.empty2 <- data.frame()

for (i in 1:20) {
  for (j in list_keywords) {
    x = sample(seq(as.Date('2019-01-01'), as.Date('2019-10-20'), by = "day"), 5)
    y = sample(seq(as.Date('2019-01-01'), as.Date('2019-10-20'), by = "day"), 5)
    z = ifelse(x > y, paste(y, x, sep = " "), 
             ifelse(x == y, paste(u, x, sep =" "),
                      paste(x, y,sep = " ")))  

    a <- gtrendsR::gtrends(keyword = j, 
                      time = z, 
                      geo = "CH",   cookie_url = "http://trends.google.com/Cookies/NID", tz = 0)

    b <- a$interest_by_region

    data.empty2 <- rbind(data.empty2, b)

    tmsleep <- sample(1:10,1) 
    Sys.sleep(tmsleep)

    if(i == 20) break

    } 
}

I have tried out all the diverse parameter in the gtrends function. The function also runs when inputting a longer list of keywords but only using one time span. Creating a date list outside of the loop and looping through it also does not seem to do the trick. Thank you for your input.

PMassicotte commented 1 year ago

Works very fine with on my side. Also, your link on Google Trends does not include multiple time spans...

library(gtrendsR)

plot(gtrends("svp", geo = "CH", time = "2019-01-01 2019-10-20"))

Created on 2023-05-03 with reprex v2.0.2