pythonpandasgoogle-trends

Pytrends Multiple geo codes


I use the codes below to export the number of searches for multiple states in US and get the error "ResponseError: The request failed: Google returned a response with code 400."

from pytrends.request import TrendReq
import pandas as pd
import time
startTime = time.time()
pytrend = TrendReq(hl='en-US', tz=360)

colnames = ["keywords"]
df = pd.read_csv("keyword_list.csv", names=colnames)
df2 = df["keywords"].values.tolist()
df2.remove("Keywords")

dataset = []

for x in range(0,len(df2)):
     keywords = [df2[x]]
     pytrend.build_payload(
     kw_list=keywords,
     cat=0,
     timeframe='2020-01-01 2020-02-01',
     geo='US-MA,US-TX,US-NY,US-WA')
     data = pytrend.interest_over_time()
     if not data.empty:
          data = data.drop(labels=['isPartial'],axis='columns')
          dataset.append(data)

result = pd.concat(dataset, axis=1)
result.to_csv("US.csv")

Solution

  • Issue is with the geo parameter, You can provide one at a time not all, You can try like this

    from pytrends.request import TrendReq
    import pandas as pd
    import time
    startTime = time.time()
    pytrend = TrendReq(hl='en-US', tz=360)
    
    colnames = ["keywords"]
    df = pd.read_csv("keyword_list.csv", names=colnames)
    df2 = df["keywords"].values.tolist()
    df2.remove("Keywords")
    
    dataset = []
    for geo_code in ['US-MA','US-TX','US-NY','US-WA']:
        for x in range(0,len(df2)):
            keywords = [df2[x]]
            pytrend.build_payload(
            kw_list=keywords,
            cat=0,
            timeframe='2020-01-01 2020-02-01',
            geo=geo_code)
            data = pytrend.interest_over_time()
            if not data.empty:
                data = data.drop(labels=['isPartial'],axis='columns')
                data['geo']=geo_code
                dataset.append(data)
    
    result = pd.concat(dataset)
    result.to_csv("US.csv")