我有一个文件,它从 python 中的 twitter 中提取信息并将状态消息运行到我的 shell 中。我想将它们从外壳中取出并放入数据库中。我不知道该怎么做。我也没有为它制作的数据库,我将在数据库堆栈上对此提出质疑。我的代码如下:
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=['search term'])
如何从流中获取信息到数据库中?我还想过滤流以只允许某些信息进入。我想进入数据库的唯一信息如下:
- 留言作者
- 信息
- 日期/时间戳
- GEO(如果有)
- 来源 ie Tweetdeck, web, mobile yaddda yadda
- 消息是 RT'd