2013-10-15 296 views
0

我寫了這個劇本獲得從高音喇叭的鳴叫:如何使用tweetstream蟒蛇

import tweetstream 
import pymongo 
connection = pymongo.Connection("mongodb://localhost:27017") 
db = connection.socialdata 
words = ["social media", "innovation", "cloud computing"] 
with tweetstream.FilterStream('username', 'password', track=words) as stream: 
    for tweet in stream:   
     db.tweets.save(tweet) 

但在執行我收到以下錯誤,請告訴我如何刪除此錯誤:

Traceback (most recent call last): 
    File "tweet.py", line 9, in <module> 
     with tweetstream.FilterStream(username, password, track=words) as stream: 
TypeError: __init__() takes at least 5 arguments (4 given)</module> 

提前致謝。

+0

你錯過了一個參數。它預計有5個參數,並且你已經給出它4.「tweetstream」,「username」,「password」和「track」。閱讀文檔以查看缺少的東西 – Paco

+0

我建議使用Twython。 –

回答

2

我想知道你是怎麼tweetstream擺在首位的工作,因爲我所知道的是,基本的用戶名/密碼訪問是deprecated一段時間回來。現在Twitter只允許OAuth訪問。


回到你的問題,你的FileStream電話是完全有效的。看看FileStream類的實現,你會明白爲什麼。

以下是從FilterStream類,這是可以here

def __init__(self, username, password, follow=None, locations=None, 
       track=None, catchup=None, raw=False, timeout=None, url=None): 
     self._follow = follow 
     self._locations = locations 
     self._track = track 
     # remove follow, locations, track 
     BaseStream.__init__(self, username, password, 
          raw=raw, timeout=timeout, url=url) 

所以,tweetstream.FilterStream("username", "password", track=words)應該工作的幾行代碼。因爲你可以看到__init__只有3個強制參數。 (自我,用戶名,密碼)。

所有其他都是可選的。請注意,此代碼來自tweetstream 1.1.1,我認爲最新版本已發佈。


然而,由於您的錯誤說,FilterStream構造函數tweetstream至少需要5個參數。

This文檔舉例說明了您正在嘗試執行的操作。

,因爲它說,請嘗試使用此初始化相反,

with tweetstream.FilterStream("username", "password", track=words, 
           follow=people, locations=locations) as stream 

據有關人士介紹,

  • 地點是在地理標記的鳴叫 應該始發的邊框列表。參數應該是 經度/緯度對的迭代。

  • 跟蹤指定要跟蹤的關鍵字。參數應該是可迭代的字符串 。

  • 跟隨返回引用給定用戶的狀態。參數應該是 是twitter用戶ID的迭代。這些ID是用戶標識,而不是 屏幕名稱。

+0

感謝您的回覆。但我嘗試了同樣的鏈接仍然得到相同的錯誤,所以有任何其他方式來做到這一點,請建議。 –

+0

@ShilpaJawale,你正在使用什麼庫?流媒體API? –

+0

我正在使用tweet流庫 –