首页 > 解决方案 > 如何将数据导出到谷歌表格

问题描述

嘿伙计们,我制作了一个抓取程序,可以从网站上抓取一些信息,现在我需要将这些信息放入谷歌表格中,我想知道最好的方法是什么?将其转换为 csv 文件,然后将其放入工作表中?或其他任何我愿意接受的建议。

标签: pythongoogle-sheetsautomation

解决方案


使用谷歌 API是更好的方法,避免资源密集型文件读/写操作。

先决条件:创建一个服务帐户并准备好密钥 json,这是 python 中的示例代码:

import gspread
from oauth2client.service_account import ServiceAccountCredentials
import datetime

SCOPE = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']
SAMPLE_SPREADSHEET_ID = 'xxxxxx'

def update_cell(cell, value):
credentials = ServiceAccountCredentials.from_json_keyfile_name('downloaded.json', SCOPE)
gc = gspread.authorize(credentials)

wks = gc.open_by_key(SAMPLE_SPREADSHEET_ID).get_worksheet(1)
wks.update_acell(cell, value)

if __name__ == '__main__':
    update_cell('A1', 'test')

推荐阅读