質問編集履歴

1

回答をいただいて進捗後のエラー内容を更新

2018/01/25 09:28

投稿

illumi
illumi

スコア8

test CHANGED
File without changes
test CHANGED
@@ -105,3 +105,167 @@
105
105
  - Jupyter Notebook
106
106
 
107
107
  - Python3
108
+
109
+
110
+
111
+
112
+
113
+ ### 追記内容
114
+
115
+ 上記解決後、引続き下記の参考記事の内容を進めている過程でもう一度エラーで進捗が止まってしまいました。
116
+
117
+ 下記の参考記事の内容を実現するために、参考記事と同様の手順で作業をしています。
118
+
119
+ [参考記事](https://qiita.com/phigasui/items/cc6209b0f36d8774ec27)
120
+
121
+
122
+
123
+ ### 発生している問題・エラーメッセージ
124
+
125
+ 「各種紹介 > 指定ページの流入元」のパートにおいて問題が発生しております。
126
+
127
+ 参考記事内にある下記のコードをJupyter Notebook上で実行したところエラーが発生しました。
128
+
129
+
130
+
131
+ #### 実行コード
132
+
133
+ ```
134
+
135
+ # サンプルファイル読み込み ※「XXXXX」部分は実際のコードから変換しています
136
+
137
+ import HelloAnalytics as HA
138
+
139
+
140
+
141
+ # 分析用モジュール
142
+
143
+ import pandas as pd
144
+
145
+ from collections import OrderedDict
146
+
147
+ import datetime
148
+
149
+ from scipy import stats
150
+
151
+ import numpy as np
152
+
153
+
154
+
155
+ scope = ['https://www.googleapis.com/auth/analytics.readonly'] # データを取得するだけならこれでOK
156
+
157
+ service_account_email = 'XXXXX@XXXXX.iam.gserviceaccount.com' # 自分のサービスアカウントEmail(~gserviceaccount.com)
158
+
159
+ key_file_location = r"C:\Users\XXXXX\python_jupyterNB\XXXXX.p12" # ダウンロードしてきた自分のキーファイルをおいたパス
160
+
161
+
162
+
163
+ service = HA.get_service('analytics', 'v3', scope, key_file_location, service_account_email)
164
+
165
+ profile = HA.get_first_profile_id(service)
166
+
167
+
168
+
169
+ page = "path/to/page"
170
+
171
+ res = service.data().ga().get(
172
+
173
+ ids='ga:' + profile,
174
+
175
+ start_date=(datetime.date.today() - datetime.timedelta(days=8)).__str__(),
176
+
177
+ end_date=(datetime.date.today() - datetime.timedelta(days=1)).__str__(),
178
+
179
+ metrics='ga:users',
180
+
181
+ dimensions='ga:date,ga:previousPagePath',
182
+
183
+ filters="ga:pagePath=~{0}".format(page)
184
+
185
+ ).execute()
186
+
187
+ uu = res['rows']
188
+
189
+ uu_df = pd.DataFrame(res['rows'])
190
+
191
+ uu = uu_df.pivot(index=0, columns=1, values=2).fillna(0).astype(int)
192
+
193
+ ```
194
+
195
+
196
+
197
+ #### エラーコード
198
+
199
+ ```
200
+
201
+ ---------------------------------------------------------------------------
202
+
203
+ UnicodeDecodeError Traceback (most recent call last)
204
+
205
+ <ipython-input-7-9409de97f5d6> in <module>()
206
+
207
+ 13 key_file_location = r"C:\Users\XXXXX\python_jupyterNB\XXXXX.p12" # ダウンロードしてきた自分のキーファイルをおいたパス
208
+
209
+ 14
210
+
211
+ ---> 15 service = HA.get_service('analytics', 'v3', scope, key_file_location)
212
+
213
+ 16 profile = HA.get_first_profile_id(service)
214
+
215
+ 17
216
+
217
+
218
+
219
+ ~\python_jupyterNB\HelloAnalytics.py in get_service(api_name, api_version, scope, key_file_location)
220
+
221
+ 27
222
+
223
+ 28 credentials = ServiceAccountCredentials.from_json_keyfile_name(
224
+
225
+ ---> 29 key_file_location, scope)
226
+
227
+ 30
228
+
229
+ 31 # Build the service object.
230
+
231
+
232
+
233
+ C:\myProgram\anaconda\lib\site-packages\oauth2client\service_account.py in from_json_keyfile_name(cls, filename, scopes, token_uri, revoke_uri)
234
+
235
+ 218 """
236
+
237
+ 219 with open(filename, 'r') as file_obj:
238
+
239
+ --> 220 client_credentials = json.load(file_obj)
240
+
241
+ 221 return cls._from_parsed_json_keyfile(client_credentials, scopes,
242
+
243
+ 222 token_uri=token_uri,
244
+
245
+
246
+
247
+ C:\myProgram\anaconda\lib\json\__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
248
+
249
+ 294
250
+
251
+ 295 """
252
+
253
+ --> 296 return loads(fp.read(),
254
+
255
+ 297 cls=cls, object_hook=object_hook,
256
+
257
+ 298 parse_float=parse_float, parse_int=parse_int,
258
+
259
+
260
+
261
+ UnicodeDecodeError: 'cp932' codec can't decode byte 0x82 in position 1: illegal multibyte sequence
262
+
263
+ ```
264
+
265
+
266
+
267
+ ### 試したこと
268
+
269
+ エラーコードなどを検索等して原因を探り、文字コードの問題かと思い設定ファイル関連を確認したりはしていますが、解決方法が分からず。。
270
+
271
+ お手数ですが、こちらのご回答お願いできますでしょうか。。