Search code examples
pythonpython-3.xpandasgoogle-search-apigoogle-search-console

Iterate a piece of code connecting to API using two variables pulled from two lists


I'm trying to run a script (API to google search console) over a table of keywords and dates in order to check if there was improvement in keyword performance (SEO) after the date.

Since i'm really clueless im guessing and trying but Jupiter notebook isn't responding so i can't even tell if im wrong...

This git was made by Josh Carty the git from which i took this code is: https://github.com/joshcarty/google-searchconsole

Already pd.read_csv the input table (consist of two columns 'keyword' and 'date'), made the columns into two separate lists (or maybe it better to use dictionary/other?):

KW_list and Date_list

I tried: for i in KW_list and j in Date_list:

for i in KW_list and j in Date_list:

 account = searchconsole.authenticate(client_config='client_secrets.json',
                                      credentials='credentials.json')
 webproperty = account['https://www.example.com/']
 report = webproperty.query.range(j, days=-30).filter('query', i, 'contains').get()
 report2 = webproperty.query.range(j, days=30).filter('query', i, 'contains').get()
 df = pd.DataFrame(report)
 df2 = pd.DataFrame(report2)
df

Expect to see the data frame of all the different keywords (keyowrd1-stat1 , keyword2 - stats2 below, etc. [no overwrite]) at the dates 30 days before the date in the neighbor cell (in the input file) or at least some respond from J.notebook so i will know what is going on.


Solution

  • Try using the zip function to combine the lists into a list of tuples. This way, the date and the corresponding keyword are combined.

    account = searchconsole.authenticate(client_config='client_secrets.json', credentials='credentials.json')
    webproperty = account['https://www.example.com/']
    df1 = None
    df2 = None
    first = True
    
    for (keyword, date) in zip(KW_list, Date_list):
       report = webproperty.query.range(date, days=-30).filter('query', keyword, 'contains').get()
       report2 = webproperty.query.range(date, days=30).filter('query', keyword, 'contains').get()
    
        if first:
            df1 = pd.DataFrame(report)
            df2 = pd.DataFrame(report2)
            first = False
        else:
            df1 = df1.append(pd.DataFrame(report))
            df2 = df2.append(pd.DataFrame(report2))