【问题标题】:How can I use Google Search Console API from Google Collaboratory environment?如何在 Google Collaboratory 环境中使用 Google Search Console API?
【发布时间】:2020-02-28 06:01:17
【问题描述】:

我想使用 Google Collaboratory 环境中的 google search console api。

我尝试了这段代码,但出现“TypeError: expected str, bytes or os.PathLike object, not dict”的错误。

我知道 files.upload() 返回的类型不符合 ServiceAccountCredentials.from_json_keyfile_name(KEY_FILE_LOCATION, SCOPES) 的预期类型。

但是,我不知道如何纠正它。

第一次在 Stack Overflow 上提问,我是编程和英语的初学者,你能轻松教我吗?

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)

【问题讨论】:

    标签: python google-api google-oauth google-search-console


    【解决方案1】:

    我解决了。 首先,我将 Key_file 上传到与该程序文件相同的目录中。 然后,我通过“挂载”连接了程序和 Google Drive 帐户。 最后,我得到了 json key_file 的绝对路径。

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-08
      • 1970-01-01
      • 2021-07-18
      • 2016-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多