我想知道的是有写入使用Python谷歌电子表格的方式。发现python-gdata-client库,所有依赖安装它。使用下面这段代码,但它无法正常工作
import time
import gdata.spreadsheet.service
email = '[email protected]'
password = 'pwd'
weight = '180'
# Find this value in the url with 'key=XXX' and copy XXX below
spreadsheet_key = 'pRoiw3us3wh1FyEip46wYtW'
# All spreadsheets have worksheets. I think worksheet #1 by default always
# has a value of 'od6'
worksheet_id = 'Sheet1'
spr_client = gdata.spreadsheet.service.SpreadsheetsService()
spr_client.email = email
spr_client.password = password
spr_client.source = 'Example Spreadsheet Writing Application'
spr_client.ProgrammaticLogin()
# Prepare the dictionary to write
dict = {}
dict['date'] = time.strftime('%m/%d/%Y')
dict['time'] = time.strftime('%H:%M:%S')
dict['weight'] = weight
print dict
entry = spr_client.InsertRow(dict, spreadsheet_key, worksheet_id)
if isinstance(entry, gdata.spreadsheet.SpreadsheetsList):
print "Insert row succeeded."
else:
print "Insert row failed."
这是错误的是说 -
Traceback (most recent call last):
File "D:/steve/test.py", line 28, in <module>
entry = spr_client.InsertRow(dict, spreadsheet_key, worksheet_id)
File "C:\Python27\lib\site-packages\gdata\spreadsheet\service.py", line 338, in InsertRow
converter=gdata.spreadsheet.SpreadsheetsListFromString)
File "C:\Python27\lib\site-packages\gdata\service.py", line 1235, in Post
media_source=media_source, converter=converter)
File "C:\Python27\lib\site-packages\gdata\service.py", line 1346, in PostOrPut
redirects_remaining - 1, media_source, converter=converter)
File "C:\Python27\lib\site-packages\gdata\service.py", line 1328, in PostOrPut
return converter(result_body)
File "C:\Python27\lib\site-packages\gdata\spreadsheet\__init__.py", line 376, in SpreadsheetsListFromString
xml_string)
File "C:\Python27\lib\site-packages\atom\__init__.py", line 92, in optional_warn_function
return f(*args, **kwargs)
File "C:\Python27\lib\site-packages\atom\__init__.py", line 126, in CreateClassFromXMLString
tree = ElementTree.fromstring(xml_string)
File "<string>", line 124, in XML
ParseError: mismatched tag: line 944, column 4
您正在使用自2012年起,似乎4月20日,这是过时ClientLogin method,谷歌已经关闭了它在2015年5月,第26条。
使用OAuth2代替。
如上面阿利克从2015年开始提到u需要使用oauth2client您可以通过运行安装:
pip install oauth2client
这里是一个示例代码登录:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
# trying to log in
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope) # u can download the json file from google's api manager
client = gspread.authorize(creds) # authorize access
# defining the sheet that we will work on
sheet = client.open('sheetName').sheet1 # getting a sheet to work on
我知道它已经过了一年,现在既然你问你的问题,但我希望我帮助别的人。
是的,这是可能使用了几包:gspread,谷歌的API的Python客户端,oauth2client,tweepy
...写一个数据帧,使用方法:gspread_dataframe
PIP安装gspread,谷歌API的Python客户端,oauth2client,tweepy,gspread_dataframe
我写了跟踪Twitter的情绪here on GitHub的脚本。