Python 如何使用Google协作环境中的Google搜索控制台API?
我想使用谷歌协作环境中的谷歌搜索控制台api 我尝试了这段代码,但得到了一个错误“TypeError:预期的str、bytes或os.PathLike对象,而不是dict” 我知道files.upload()返回的类型不适合ServiceAccountCredentials.from_json_keyfile_name(KEY_FILE_位置,作用域)的预期类型 然而,我不知道如何纠正它 这是我第一次在Stack Overflow中提问,我是编程和英语的初学者,所以你能简单地教我吗Python 如何使用Google协作环境中的Google搜索控制台API?,python,google-api,google-oauth,google-search-console,Python,Google Api,Google Oauth,Google Search Console,我想使用谷歌协作环境中的谷歌搜索控制台api 我尝试了这段代码,但得到了一个错误“TypeError:预期的str、bytes或os.PathLike对象,而不是dict” 我知道files.upload()返回的类型不适合ServiceAccountCredentials.from_json_keyfile_name(KEY_FILE_位置,作用域)的预期类型 然而,我不知道如何纠正它 这是我第一次在Stack Overflow中提问,我是编程和英语的初学者,所以你能简单地教我吗 import
import pandas as pd
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
SCOPES = ['https://www.googleapis.com/auth/webmasters.readonly']
from google.colab import files
uploaded = files.upload()
KEY_FILE_LOCATION = uploaded
credentials = ServiceAccountCredentials.from_json_keyfile_name(KEY_FILE_LOCATION, SCOPES)
webmasters = build('webmasters', 'v3', credentials=credentials)
url = '*********'
d_list = ['query', 'page']
start_date = '2019-12-01'
end_date = '2019-12-31'
row_limit = 5000
body = {
'startDate': start_date,
'endDate': end_date,
'dimensions': d_list,
'rowLimit': row_limit
}
response = webmasters.searchanalytics().query(siteUrl=url, body=body).execute()
df = pd.io.json.json_normalize(response['rows'])
for i, d in enumerate(d_list):
df[d] = df['keys'].apply(lambda x: x[i])
df.drop(columns='keys', inplace=True)
df.to_csv('{}.csv'.format(start_date), index=False)
print(df)
我解决了。
首先,我将Key_文件上载到与此程序文件相同的目录中。
然后,我通过“挂载”将程序和Google Drive帐户连接起来。
最后,我得到了json密钥文件的绝对路径
import pandas as pd
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
SCOPES = ['https://www.googleapis.com/auth/webmasters.readonly']
from google.colab import drive
drive.mount('/content/drive')
KEY_FILE_LOCATION = '/content/drive/My Drive/<path-to-json>'
credentials = ServiceAccountCredentials.from_json_keyfile_name(KEY_FILE_LOCATION, scopes=SCOPES)
webmasters = build('webmasters', 'v3', credentials=credentials)
url = '*********'
d_list = ['query', 'page']
start_date = '2019-12-01'
end_date = '2019-12-31'
row_limit = 5000
body = {
'startDate': start_date,
'endDate': end_date,
'dimensions': d_list,
'rowLimit': row_limit
}
response = webmasters.searchanalytics().query(siteUrl=url, body=body).execute()
df = pd.io.json.json_normalize(response['rows'])
for i, d in enumerate(d_list):
df[d] = df['keys'].apply(lambda x: x[i])
df.drop(columns='keys', inplace=True)
df.to_csv('{}.csv'.format(start_date), index=False)
print(df)
将熊猫作为pd导入
从GoogleAppClient.discovery导入生成
从oauth2client.service\u帐户导入ServiceAccountCredentials
作用域=['https://www.googleapis.com/auth/webmasters.readonly']
从google.colab导入驱动器
安装(“/content/drive”)
KEY_FILE_LOCATION='/content/drive/My drive/'
credentials=ServiceAccountCredentials.from_json_keyfile_name(KEY_FILE_位置,scopes=scopes)
网站管理员=构建('webmasters','v3',凭据=凭据)
url='***********'
d_list=['query','page']
开始日期='2019-12-01'
结束日期='2019-12-31'
行限制=5000
正文={
“开始日期”:开始日期,
“结束日期”:结束日期,
“维度”:d_列表,
“行限制”:行限制
}
response=webmasters.searchanalytics().query(siteUrl=url,body=body).execute()
df=pd.io.json.json_规范化(响应['rows'])
对于枚举中的i,d(d_列表):
df[d]=df['keys'].apply(λx:x[i])
drop(columns='keys',inplace=True)
df.to_csv('{}.csv'.format(start_date),index=False)
打印(df)
我解决了它。
首先,我将Key_文件上载到与此程序文件相同的目录中。
然后,我通过“挂载”将程序和Google Drive帐户连接起来。
最后,我得到了json密钥文件的绝对路径
import pandas as pd
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
SCOPES = ['https://www.googleapis.com/auth/webmasters.readonly']
from google.colab import drive
drive.mount('/content/drive')
KEY_FILE_LOCATION = '/content/drive/My Drive/<path-to-json>'
credentials = ServiceAccountCredentials.from_json_keyfile_name(KEY_FILE_LOCATION, scopes=SCOPES)
webmasters = build('webmasters', 'v3', credentials=credentials)
url = '*********'
d_list = ['query', 'page']
start_date = '2019-12-01'
end_date = '2019-12-31'
row_limit = 5000
body = {
'startDate': start_date,
'endDate': end_date,
'dimensions': d_list,
'rowLimit': row_limit
}
response = webmasters.searchanalytics().query(siteUrl=url, body=body).execute()
df = pd.io.json.json_normalize(response['rows'])
for i, d in enumerate(d_list):
df[d] = df['keys'].apply(lambda x: x[i])
df.drop(columns='keys', inplace=True)
df.to_csv('{}.csv'.format(start_date), index=False)
print(df)
将熊猫作为pd导入
从GoogleAppClient.discovery导入生成
从oauth2client.service\u帐户导入ServiceAccountCredentials
作用域=['https://www.googleapis.com/auth/webmasters.readonly']
从google.colab导入驱动器
安装(“/content/drive”)
KEY_FILE_LOCATION='/content/drive/My drive/'
credentials=ServiceAccountCredentials.from_json_keyfile_name(KEY_FILE_位置,scopes=scopes)
网站管理员=构建('webmasters','v3',凭据=凭据)
url='***********'
d_list=['query','page']
开始日期='2019-12-01'
结束日期='2019-12-31'
行限制=5000
正文={
“开始日期”:开始日期,
“结束日期”:结束日期,
“维度”:d_列表,
“行限制”:行限制
}
response=webmasters.searchanalytics().query(siteUrl=url,body=body).execute()
df=pd.io.json.json_规范化(响应['rows'])
对于枚举中的i,d(d_列表):
df[d]=df['keys'].apply(λx:x[i])
drop(columns='keys',inplace=True)
df.to_csv('{}.csv'.format(start_date),index=False)
打印(df)