from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time
s=raw_input('string');
try:
file = open('keyword.txt', 'r')
keyword=file.read()
ckey ='CONSUMER KEY'
csecret ='CONSUMER SECRET'
atoken ='ACCESS TOKEN'
asecret='ACCESS SECRET'
class listener(StreamListener):
def on_data(self, data):
# print data
tweet=data.split(',"text":"')[1].split('","source')[0]
print (tweet)
print ('\n')
saveThis=tweet
saveFile =open('projectdatabase.csv','a')
saveFile.write(saveThis )
saveFile.write('\n')
saveFile.close()
return True
def on_error(self, status):
print (status)
auth=OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream=Stream(auth, listener())
twitterStream.filter(track=[keyword])
except Exception:
答案 0 :(得分:0)
您的问题需要更具描述性,但是如果您想使用搜索获得特定数量的推文......您可以参考此示例
导入tweepy import csv #Import csv 导入json 导入编解码器 导入日期时间
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
#Access tokens
auth = tweepy.auth.OAuthHandler('#######', '#######')
auth.set_access_token('########', '######')
api = tweepy.API(auth)
# Open/Create a file to append data
csvFile = open('result1234.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)
f = codecs.open('abc.csv',encoding='utf-8', mode='a')
for tweet in tweepy.Cursor(api.search,
q="Interstellar",
since="2015-01-30",
until="2015-02-01",
lang="en").items(10000):
#Write a row to the csv file/ I use encode utf-8
print tweet;
csvWriter.writerow([tweet])