使用Python读取Google Spreadsheet的内容并写入到mangodb

昨天突发奇想, 现在google docs里的spreadsheet很强大, 如果我的网站数据不是很大, 那么我通过spreadsheet来保存我的数据不是一件很方便的事情吗, 事实上, 我的网站也只是保存一些产品的数据, 而我的产品又不可能像eBay那样有许多, 那么我就试试把数据保存在Google Spreadsheet里吧。 

目标有了, 工具呢? 去看看Google Spreadsheets API吧, 嗯, 它还躺在Google Labs里面, 不过也还不错, 那么它支持那些语言呢?因为我对Python很有爱, 而且这个API也支持Python, perfect!!! 好了, 开始动手。 如果要使用这个API, 那么第一步自然是下载一个叫Google Python Client Library东西。 使用这个client的前提是你要有Python的环境, 并且包含httplib和urllib这两个模块, 还好python2.6都内置了这两个模块, 如果你使用2.6这个版本的话, 直接从上面这个link下载, 安装就好了。 

 python setup.py install

进入python, 我们做一些简单的实验。

>>>   import  gdata.spreadsheet.service as service
>>>  client  =  service.SpreadsheetsService()
>>>  client.email  =   " [email protected] "    # 你的app帐号或者gmail帐号
>>>  client.password  =   " ******** "    # 这个就是密码的东西, 你当然看不见了
>>>  client.source  =   " iMaQ-Documents-v1 "   # 这个不好解释, 下面大概描述一下
>>>  client.ProgrammaticLogin()  # 利用上面的信息登陆到Google

嗯, 很好, 我in了 xD, 接下来, 我要知道我的Documents里有哪些spreadsheet, 废话, 我当然知道, 我可以直接进到我的documents里去看我有哪些表格, 我们就以里面自带的一个demo的文档做实验, 那个叫做internal-Contact的东东。 首先如果要访问这个文档的话, 我们要知道这个文档的key

>>>  docs  =  client.GetSpreadsheetsFeed()
>>>   for  i, entry  in  enumerate(docs.entry):
...     
print  entry.title.text , entry.id.text
... 
internal 
-  Contact http: // spreadsheets.google.com / feeds / spreadsheets / private / full / blablablabla__Y1ax999C
products http:
// spreadsheets.google.com / feeds / spreadsheets / private / full / blablablablabla_Z1fSm - nXQ
>>>

 看到了吗? 列出了两个我这个帐号可以访问的文档, entry.title.text就是文档的名字, entry.id.text就是这个文档的feed地址, 是你后面调用api的时候直接访问这个文档的地址, 里面的/full/后面的一串blablabla就是这个文档的key. 有了这个key, 我们就可以直接去访问这个文档里的内容啦, 首先看看这个表格里有几个sheet。

>>>  sheet  =  client.GetWorksheetsFeed(key)
>>>   for  i, entry  in  enumerate(sheet.entry):
...     
print  entry.title.text, entry.id.text
... 
Sheet1 http:
// spreadsheets.google.com / feeds / worksheets / blablablablabla__Y1ax999C / private / full / od1
>>>

 结果给出只有一个Sheet1, 后面的那个是workbook的key和这个sheet的id, 这里是od1, 现在我们就可以去看看这个sheet里的内容了

>>>  table  =  client.GetListFeed(key,wksht_id)
>>>   for  i, entry  in  enumerate(table.entry):
...     
for  column  in  entry.custom:
...             
print   " [%s:%s] " % (column, entry.custom[column].text)
... 
[comment:I
' m commenting on this contact form]
[timestamp: 2 / 15 / 2010   8 : 07 : 30 ]
[name:Sarah]
[email:[email protected]]
[comment:I want to join the group.]
[timestamp:
2 / 15 / 2010   13 : 06 : 33 ]
[name:Larisa]
[email:[email protected]]
[comment:
is  it wotk]
[timestamp:
2 / 15 / 2010   17 : 23 : 55 ]
[name:Larisa]
[email:[email protected]]
[comment:Test]
[timestamp:
2 / 16 / 2010   12 : 17 : 22 ]
[name:Jeremy]
... 
# 后面的省略掉了

内容都出来了, 好多阿 - . -!

看来访问内容还是很容易的, 接下来, 我们就可以把内容写到可爱的mongodb里 去了, 如何使用mongodb就不是本文所涉及的主题了, 既然内容已经被都出来了, 你可以将这些内容写到你喜欢的地方去, big table, mysql, oracle, 或者txt里。 使用mongodb的前提是你已经有了mongo的环境并且装好了python driver, 叫pymongo, 那么下面就接着继续

>>>   from  pymongo  import  Connection
>>>  db  =  Connection( " localhost " ).test.contacts
>>>   for  i, entry  in  enumerate(table.entry):
...     obj 
=  {}
...     
for  col  in  entry.custom:
...             obj[col] 
=  entry.custom[col].text
...     db.save(obj)
... 
ObjectId(
' 4c2f9bad092ce508b2000000 ' )
ObjectId(
' 4c2f9bad092ce508b2000001 ' )
ObjectId(
' 4c2f9bad092ce508b2000002 ' )
ObjectId(
' 4c2f9bad092ce508b2000003 ' )
ObjectId(
' 4c2f9bad092ce508b2000004 ' )
ObjectId(
' 4c2f9bad092ce508b2000005 ' )
ObjectId(
' 4c2f9bad092ce508b2000006 ' )
ObjectId(
' 4c2f9bad092ce508b2000007 ' )
ObjectId(
' 4c2f9bad092ce508b2000008 ' )
ObjectId(
' 4c2f9bad092ce508b2000009 ' )
ObjectId(
' 4c2f9bad092ce508b200000a ' )
ObjectId(
' 4c2f9bad092ce508b200000b ' )
ObjectId(
' 4c2f9bad092ce508b200000c ' )
...

 好了, 刚才那个contacts里的内容就写到mongodb里去了, 你可以进到mongo的console里去

use test
db.contacts.find()

转载于:https://www.cnblogs.com/Stephen/archive/2011/03/06/python-sync-gspreadsheet-mongodb.html

猜你喜欢

转载自blog.csdn.net/weixin_34293902/article/details/93754638