嗨!你知道吗
我遇到了一些关于Python的tweepy库的问题。我第一次启动下面的脚本,一切都很完美,第二次。。。脚本意外停止。你知道吗
我没有发现任何关于此行为的信息,侦听器在几秒钟后停止,并且我没有任何错误代码或其他信息。你知道吗
有一个简单的代码:
import tweepy
import sys
import json
from textwrap import TextWrapper
from datetime import datetime
from elasticsearch import Elasticsearch
consumer_key = "hidden"
consumer_secret = "hidden"
access_token = "hidden"
access_token_secret = "hidden"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
ES_HOST = {"host" : "localhost", "port" : 9200}
es = Elasticsearch(hosts = [ES_HOST])
class StreamListener(tweepy.StreamListener):
print('Starting StreamListener')
status_wrapper = TextWrapper(width=60, initial_indent=' ', subsequent_indent=' ')
def on_status(self, status):
try:
print 'n%s %s' % (status.author.screen_name, status.created_at)
json_data = status._json
#print json_data['text']
es.create(index="idx_twp",
doc_type="twitter_twp_nintendo",
body=json_data
)
except Exception, e:
print e
pass
print('Starting Receiving')
streamer = tweepy.Stream(auth=auth, listener=StreamListener(), timeout=3000000000)
#Fill with your own Keywords bellow
terms = ['nintendo']
streamer.filter(None,terms)
#streamer.userstream(None)
print ('Ending program')
然后是输出(只有2秒)
[root@localhost ~]# python projects/m/twitter/twitter_logs.py
Starting StreamListener
Starting Receiving
Ending program
我使用的是python2.7.5
有什么想法吗?你知道吗
嗨!你知道吗
我通过virtualenv将Python版本更改为3.5,解决了这个奇怪的问题。目前,效果不错。你知道吗
这可能是由于python版本造成的,无论如何,如果有人有这个版本,我建议使用virtualenv测试另一个python版本,看看会发生什么。你知道吗
仅供参考:我已经在github项目中打开了问题#759。你知道吗
相关问题 更多 >
编程相关推荐