使用python将google docs公共电子表格下载到csv [英] Download google docs public spreadsheet to csv with python
本文介绍了使用python将google docs公共电子表格下载到csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以用wget下载谷歌文档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,如Gecko)Chrome / 24.0.1284.0 Safari / 537.13')
opener = urllib2.build_opener()
data = opener.open(request).read()
打印数据
结果是google登录页面。我在做什么错了?
解决方案
使用请求,比使用urllib好得多。 b
试试这个。
导入请求
response = requests.get('https: //docs.google.com/spreadsheet/ccc?key=0ArM5yzzCw9IZdEdLWlpHT1FCcUpYQ2RjWmZYWmNwbXc&output=csv')
assert response.status_code == 200,'错误的状态码'
print response.content
I can download a google docs csv with wget:
wget --no-check-certificate --output-document=locations.csv 'https://docs.google.com/spreadsheet/ccc?key=0ArM5yzzCw9IZdEdLWlpHT1FCcUpYQ2RjWmZYWmNwbXc&output=csv'
But I cant download the same csv with python:
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
The result is the google login page. What am i doing wrong?
解决方案
Just use requests, it is way better than using urllib.
Try this.
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
这篇关于使用python将google docs公共电子表格下载到csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文