2015-04-15 34 views
1

我有一個項目來練習我的Python技能:修改tweepy流類

  1. 提取一些鳴叫與Tweepy流座標
  2. 將它們放到一個谷歌電子表格
  3. 然後使用谷歌電子表格在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() 
+1

不應該是'str(self.n)'? – jonrsharpe

+0

謝謝您的建議! 我剛試過。仍然一樣... – Nael

回答

2

我有麻煩這個測試我自己(主要是因爲我不熟悉tweepy.Stream是如何工作的,我認爲),但它看起來像你的MyStream實例從未得到其wks屬性放在第一位置。

這意味着,當你是指self.wks,很可能會引發AttributeError,但你永遠看不到,因爲你的try/except塊吧。 (順便說一句,這就是爲什麼except: pass往往是如此難以解決。)

你可能會希望MyStream採取額外wks說法,像這樣:

def __init__(self, wks): 
    tweepy.StreamListener.__init__(self) 

    # Store the worksheet on this instance. 
    self.wks = wks 

    # I added this to have a counter. 
    self.n = 2 

,然後更改線你實例MyStream所以你現在傳遞工作作爲參數:

stream = tweepy.Stream(auth, MyStream(wks), timeout=50) 
+0

非常感謝您的回答! 不幸的是,它給了我相同的結果。 :( – Nael

+1

你可以在'on_status'中刪除'try' /'except'行,併發布那麼會發生什麼嗎?我假設有一個錯誤會提供一些有用的信息,但是'except:pass'部分會拋出 – myersjustinc

2

我找到了答案!

其實,@jonrsharpe和@myersjustinc,你們都是對的!

「wks」沒有正確設置,我沒有正確使用「self」。

謝謝!你的提示幫助我找到答案!

編輯:所以這裏是工作代碼。

class MyStream(tweepy.StreamListener): 
def __init__(self): 
    tweepy.StreamListener.__init__(self) 

    # I added self wks but also the login step on the same line 
    self.wks = gspread.login('EMAIL', 'PASSWORD').open('SPREADSHEET').sheet1 

    # 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(self.n)), longitude) 
     self.wks.update_acell(('B' + str(self.n)), latitude) 
     print "Spreadsheet updated!" 

     # This is for my counter 
     self.n += 1 
+1

你應該發佈你的答案或接受一個! –

+0

哦!好的。我正在編輯我的帖子吧。對不起!我是一名Stackoverflow新手!:) – Nael