我有一個項目來練習我的Python技能:修改tweepy流類
- 提取一些鳴叫與Tweepy流座標
- 將它們放到一個谷歌電子表格
- 然後使用谷歌電子表格在CartoDB創建地圖
我已經可以獨立完成所有這些工作。現在,挑戰是讓所有的東西一起工作! :)
要更新我的Google Spreadsheet,我正在使用gspread。
但是,更新細胞,我需要指出的行和單元格的列是這樣的:
worksheet.update_acell('B1', 'Bingo!')
我想在我的腳本提取鳴叫的計數器。我們的目標是在每次發現推文時讓B1變爲B2,然後是B3,然後是B4。
但它不工作......座標打印在我的終端上,但就是這樣。
我想我沒有按照我應該的來使用這個類。但我不明白我的錯誤在哪裏!
幫助?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tweepy
import gspread
import time
CONSUMER_KEY, CONSUMER_SECRET = 'SECRET', 'SECRET'
USER_KEY, USER_SECRET = 'SECRET', 'SECRET'
class MyStream(tweepy.StreamListener):
def __init__(self):
tweepy.StreamListener.__init__(self)
# I added this to have a counter.
self.n = 2
def on_status(self, tweet):
try:
longitude = str(tweet.coordinates['coordinates'][0])
latitude = str(tweet.coordinates['coordinates'][1])
print longitude
print latitude
# I added this to update my google spreadsheet with the coordinates
self.wks.update_acell(('A' + str(n)), longitude)
self.wks.update_acell(('B' + str(n)), latitude)
print "Spreadsheet updated!"
# This is for my counter
self.n += 1
except:
pass
def main():
#I added these two lines to connect to my google spreadsheet
gc = gspread.login('EMAIL', 'PASSWORD')
wks = gc.open('SPREADSHEET_NAME').sheet1
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(USER_KEY, USER_SECRET)
stream = tweepy.Stream(auth, MyStream(), timeout=50)
stream.filter(locations=[-74.00,45.40,-73.41,45.72])
if __name__ == "__main__":
main()
不應該是'str(self.n)'? – jonrsharpe
謝謝您的建議! 我剛試過。仍然一樣... – Nael