如何解决Argparse - AttributeError: 'Namespace' 对象没有属性
我知道已经有很多关于这些错误的帖子了,但我似乎仍然无法弄清楚我的。我正在使用 Google Analytics Reporting API (v4) 并结合我从 Google 和 this site 找到的脚本。
有问题的代码只是尝试初始化 Google Analytics 并创建一个名称中包含脚本参数的 csv 文件,数据将在代码中进一步保存到该文件中。一切正常,直到我添加了创建文件的行(var basic_url ...)
代码运行如下:filename.py 'website_url.com' 'start_date' 'end_date'
import argparse
import csv
import sys
from apiclient.discovery import build
import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools
from googleapiclient import sample_tools
from googleapiclient import errors
# 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).'))
argparser.add_argument('start_date',help=('Start date of the requested date range in '
'YYYY-MM-DD format.'))
argparser.add_argument('end_date',help=('End date of the requested date range in '
'YYYY-MM-DD format.'))
SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
# Path to client_secrets.json file for initializing google analytics object
CLIENT_SECRETS_PATH = 'analytics_client_secrets.json'
VIEW_ID = 'XXXXXXX'
def initialize_analyticsreporting(argv,):
"""Initializes the analyticsreporting service object.
Returns:
analytics an authorized analyticsreporting service object.
"""
# Parse command-line arguments.
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,parents=[tools.argparser])
flags = parser.parse_args([])
# Set up a Flow object to be used if we need to authenticate.
flow = client.flow_from_clientsecrets(
CLIENT_SECRETS_PATH,scope=SCOPES,message=tools.message_if_missing(CLIENT_SECRETS_PATH))
# Prepare credentials,and authorize HTTP object with them.
# If the credentials don't exist or are invalid run through the native client
# flow. The Storage object will ensure that if successful the good
# credentials will get written back to a file.
storage = file.Storage('analyticsreporting.dat')
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = tools.run_flow(flow,storage,flags)
http = credentials.authorize(http=httplib2.Http())
# Build the service object.
analytics = build('analyticsreporting','v4',http=http)
basic_url = flags.property_uri
basic_url = basic_url.replace("://","_")
basic_url =basic_url.replace(".","-")
# Create blank csv file
f= open("./Landingpage_analytics_"+flags.start_date+"_"+flags.end_date+"_"+basic_url+".csv",'wt')
writer = csv.writer(f)
writer.writerow( ('Landingpage','Sessions','Bounces','avg. Session Duration','avg. Pages/Session') )
f.close()
return analytics,flags
我特别困惑,因为我在同一个脚本中使用 Google Search Console API 并使用以下几行初始化 var flags
并且访问参数没有问题,例如 flags.start_date
和 { {1}} ...
flags.property_uri
这是回溯:
service,flags = sample_tools.init(
argv,'searchconsole','v1',__doc__,__file__,parents=[argparser],scope='https://www.googleapis.com/auth/webmasters.readonly')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。