"stackoverflow"라고 표시된 트윗을 성공적으로 수집하는 작업 스크립트가 있습니다. 그러나 iPython에서 스크립트를 실행하고 싶습니다 (별도의 .py 파일이 아닌 임원). 이상적으로, 나는 ipyb 파일을 열어서 모두 실행을 선택하고 일주일 정도 (물론 랩톱을 닫지 않고) 실행하게하고 결과적으로 나는 1 주일의 짹짹이있는 .json 파일을 갖게된다.tweepy 트위터 스트림을 파일에 저장하는 방법은 무엇입니까?
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
access_token = "x"
access_token_secret = "x"
consumer_key = "x"
consumer_secret = "x"
# file name that you want to open is the second argument
save_file = open('data.json', 'a')
class listener(StreamListener):
def on_data(self, data):
print(data)
return True
def on_error(self, status):
print(status)
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["stackoverflow"])