0

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.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
Narnik Gamarnik
  • 1,049
  • 1
  • 16
  • 35
  • How exactly do you invoke this from the command line? Do you have to pass it any arguments? – gilch Jul 07 '18 at 23:43
  • look at `sys.argv`. The error usage looks it's produced by the jupyter server, not your parser. This has been asked before, I don't think you can take commadline input in a notebook – hpaulj Jul 08 '18 at 00:02
  • https://stackoverflow.com/q/50763033/901925 – hpaulj Jul 08 '18 at 00:04

1 Answers1

1

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.

gilch
  • 10,813
  • 1
  • 23
  • 28