我发现下面这段代码非常适合让我在Python Shell中查看twitter firehose的标准1%:
import sys
import tweepy
consumer_key=""
consumer_secret=""
access_key = ""
access_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
print status.text
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True # Don't kill the stream
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True # Don't kill the stream
sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
sapi.filter(track=['manchester united'])
Run Code Online (Sandbox Code Playgroud)
如何添加过滤器以仅解析特定位置的推文?我见过人们将GPS添加到其他与Twitter相关的Python代码中,但我无法在Tweepy模块中找到任何特定于sapi的内容.
有任何想法吗?
谢谢
使用 tweepy 运行 python 脚本,该脚本在英语推文的随机样本中流式传输(使用 twitter 流 API)一分钟,然后交替搜索(使用 twitter 搜索 API)一分钟,然后返回。我发现的问题是,大约 40 秒后,流媒体崩溃并出现以下错误:
完整错误:
urllib3.exceptions.ProtocolError: ('连接中断:IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))
读取的字节数可以在 0 到 1000 之间变化。
第一次看到流过早中断,搜索功能提前启动,搜索功能完成后,它再次返回流,第二次再次出现此错误时,代码崩溃。
我正在运行的代码是:
# Handles date time calculation
def calculateTweetDateTime(tweet):
tweetDateTime = str(tweet.created_at)
tweetDateTime = ciso8601.parse_datetime(tweetDateTime)
time.mktime(tweetDateTime.timetuple())
return tweetDateTime
# Checks to see whether that permitted time has past.
def hasTimeThresholdPast():
global startTime
if time.clock() - startTime > 60:
return True
else:
return False
#override tweepy.StreamListener to add logic to on_status
class StreamListener(StreamListener):
def on_status(self, …Run Code Online (Sandbox Code Playgroud)