Reputation: 1121
This code works fine if I run it from the command line.
import argparse
import sys
from googleapiclient import sample_tools
from pprint import pprint
def execute_request(service, property_uri, request):
"""Executes a searchAnalytics.query request.
Args:
service: The webmasters service to use when executing the query.
property_uri: The site or app URI to request data for.
request: The request to be executed.
Returns:
An array of response rows.
"""
return service.searchanalytics().query(
siteUrl=property_uri, body=request).execute()
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument('property_uri', type=str,
help=('Site or app URI to query data for (including '
'trailing slash).'),
action='store_true')
argparser.add_argument('start_date', type=str,
help=('Start date of the requested date range in '
'YYYY-MM-DD format.'),
action='store_true')
argparser.add_argument('end_date', type=str,
help=('End date of the requested date range in '
'YYYY-MM-DD format.'),
action='store_true')
service, flags = sample_tools.init(
sys.argv, 'webmasters', 'v3', __doc__, 'client_secrets.json', parents=[argparser],
scope='https://www.googleapis.com/auth/webmasters.readonly')
# First run a query to learn which dates we have data for. You should always
# check which days in a date range have data before running your main query.
# This query shows data for the entire range, grouped and sorted by day,
# descending; any days without data will be missing from the results.
request = {
'startDate': flags.start_date,
'endDate': flags.end_date,
'dimensions': ['date']
}
response = execute_request(service, flags.property_uri, request)
pprint(response)
If I run it from ipython notebook, I get this error:
ArgumentParser(prog='-c', usage=None, description=None, version=None, formatter_class=, conflict_handler='error', add_help=False)
usage: -c [-h] [--auth_host_name AUTH_HOST_NAME] [--noauth_local_webserver] [--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]] [--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}] property_uri start_date end_date -c: error: too few arguments
An exception has occurred, use %tb to see the full traceback.
Upvotes: 0
Views: 1545
Reputation: 11681
Just like the error message said, you gave it too few arguments. You can probably add
import sys
sys.argv = ['scriptname.py', 'argument1', ...]
at the top of the script to make it run in Jupyter. Use whatever arguments work in the command line version.
Upvotes: 1