我有一个 python 项目,我试图使用 Python 2.7 从 IDLE 运行当我运行程序时,确实会按照我的意愿创建一个文本文件,但没有写入任何信息,我不明白为什么会这样. 我通过在我的 Ubuntu 12.04 LTS 笔记本电脑上按 IDLE 中的 F5 键将它作为一个模块运行。
这是代码:
import time
import MySQLdb
import tweepy
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
# Go to http://dev.twitter.com and create an app.
# The consumer key and secret will be generated for you after
consumer_key=" # Omitted "
consumer_secret=" # Omitted "
# After the step above, you will be redirected to your app's page.
# Create an access token under the the "Your access token" section
access_token=" # Omitted "
access_token_secret=" # Omitted "
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# If the authentication was successful, you should
# see the name of the account print out
print api.me().name
class StdOutListener(StreamListener):
""" A listener handles tweets are the received from the stream.
This is a basic listener that just prints received tweets to stdout.
"""
def on_data(self, data):
print data
return True
def on_error(self, status):
print status
if __name__ == '__main__':
l = StdOutListener()
stream = Stream(auth, l)
stream.filter(track=['#google'])
如果有人想在 github 上与我一起工作,我在 github 上的文件: stocktwitterdb.py
可以在 github 上找到使用 tweepy 的流式传输示例: tweepy streaming.py
现在我在 shell 中遇到了一些事情,我想将它们放入数据库或文本文件中。