python - 如何将数据导出到谷歌表格
问题描述
嘿伙计们,我制作了一个抓取程序,可以从网站上抓取一些信息,现在我需要将这些信息放入谷歌表格中,我想知道最好的方法是什么?将其转换为 csv 文件,然后将其放入工作表中?或其他任何我愿意接受的建议。
解决方案
使用谷歌 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')
推荐阅读
- python - 在 Python 中定义一个将第一个数字加倍并将第二个数字加倍的函数
- kubernetes - Kubernetes CronJob 多个调度时间
- mysql - 合并mysql中同一张表的所有父子关系数据
- javascript - 数组中多个对象的总和值
- php - woocommerce 类别在某些日子可见
- c++ - C++ 中的 Pairs 程序集
- ruby-on-rails - 你如何确保你不在生产环境中?
- c# - 连接到 xamarin 中的 sql server
- python - 解决新的 pip 回溯运行时问题
- laravel - Laravel - 单击上一个按钮时将用户重定向回所有数据完整的另一个表单