python - 使用python将google docs公共(public)电子表格下载到csv

标签 python google-sheets

我可以使用 wget 从 Google 文档下载 CSV 文件:

wget --no-check-certificate --output-document=locations.csv 'https://docs.google.com/spreadsheet/ccc?key=0ArM5yzzCw9IZdEdLWlpHT1FCcUpYQ2RjWmZYWmNwbXc&output=csv'

但我无法使用 Python 下载相同的 csv:

import urllib2

request = urllib2.Request('https://docs.google.com/spreadsheet/ccc?key=0ArM5yzzCw9IZdEdLWlpHT1FCcUpYQ2RjWmZYWmNwbXc&output=csv')
request.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.13 (KHTML, like Gecko) Chrome/24.0.1284.0 Safari/537.13')
opener = urllib2.build_opener()
data = opener.open(request).read()
print(data)

结果是 Google 登录页面。我做错了什么?

最佳答案

只需使用 requests ,它比使用 urllib 好得多:

import requests
response = requests.get('https://docs.google.com/spreadsheet/ccc?key=0ArM5yzzCw9IZdEdLWlpHT1FCcUpYQ2RjWmZYWmNwbXc&output=csv')
assert response.status_code == 200, 'Wrong status code'
print(response.content)

你可以安装它

pip install requests

关于python - 使用python将google docs公共(public)电子表格下载到csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12842341/

相关文章:

javascript - 有没有办法检查单元格内的单词?

java - Google Sheets API 的表格是否自动更新

google-sheets - 将可能的组合生成到 Google 表格中的单独列中

javascript - 为什么在 for 循环条件中添加删除索引会导致嵌套 if 语句出错?

python - 使用 getOpenFileNames 填充 QListWidget

python - 使用 lxml 将多个元素添加到 xml

javascript - 参数列表 Google 脚本后缺少 )

python - datetime 到 Unix 时间戳,精度为毫秒

python - 直接打开/加载图像作为 numpy ndarray

python - 自定义 scikit 编码器抛出转换错误