Я хочу использовать API консоли поиска, и поэтому у меня есть кусок кода. Однако я не могу избавиться от ошибки: 'TypeError: 'HttpRequest' object is not subscriptable
'
Чего я хочу добиться: получить больше данных из Google Search Console.
Я использую последнюю версию Spyder в сочетании с последней Anaconda , Python 3.7
Мой код:
import pandas as pd
import datetime
import httplib2
import pyasn1_modules
from googleapiclient.discovery import build
from collections import defaultdict
from dateutil import relativedelta
import argparse
from oauth2client import client
from oauth2client import file
from oauth2client import tools
CLIENT_SECRETS_PATH = r'gsc_client_secret.json'
site = 'https://www.XXX.nl'
SCOPES = ['https://www.googleapis.com/auth/webmasters.readonly']
DISCOVERY_URI = ('https://www.googleapis.com/discovery/v1/apis/customsearch/v1/rest')
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
parents=[tools.argparser])
flags = parser.parse_args([])
flow = client.OAuth2WebServerFlow(client_id=XXX.apps.googleusercontent.com',
client_secret='XXX',
scope=SCOPES)
storage = file.Storage(r'C:\\Users\\XXX\\Anaconda3\\Lib\\site-packages\\bs4\\searchconsolereporting.dat')
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = tools.run_flow(flow, storage, flags)
http = credentials.authorize(http=httplib2.Http())
webmasters_service = build('webmasters', 'v3', http=http)
end_date = datetime.date.today()-relativedelta.relativedelta(days=3)
start_date = end_date
def execute_request(service, property_uri, request):
return service.searchanalytics().query(siteUrl=property_uri, body=request)
maxRows = 25000;
i = 0;
numRows = maxRows;
scDict = defaultdict(list);
while (numRows == 25000) :
request = {
'startDate': datetime.datetime.strftime(start_date,"%Y-%m-%d"),
'endDate': datetime.datetime.strftime(end_date,'%Y-%m-%d'),
'dimensions': ['date','page','query'],
'rowLimit': maxRows,
'startRow': i*maxRows
}
response = execute_request(webmasters_service, site, request)
for row in response['rows']:
scDict['date'].append(row['keys'][0] or 0)
scDict['page'].append(row['keys'][1] or 0)
scDict['query'].append(row['keys'][2] or 0)
scDict['clicks'].append(row['clicks'] or 0)
scDict['ctr'].append(row['ctr'] or 0)
scDict['impressions'].append(row['impressions'] or 0)
scDict['position'].append(row['position'] or 0)
df = pd.DataFrame(data = scDict)
df['clicks'] = df['clicks'].astype('int')
df['ctr'] = df['ctr']*100
df['impressions'] = df['impressions'].astype('int')
df['position'] = df['position'].round(2)
df.sort_values('clicks',inplace=True,ascending=False)
numRows=len(response['rows'])
i=i+1
df.to_csv(r"XXX\\gsc_api_exports\\GSC_backups.csv",index=False)
##currentcsv=pd.read_csv(r"XXX\\gsc_api_exports\\GSC_backups.csv")
##currentcsv=currentcsv.append(df,ignore_index=True)
##currentcsv.to_csv(r"XXX\\gsc_api_exports\\GSC_backups.csv",index=False)
Кто-нибудь знает, как это исправить?