Created
May 23, 2018 07:37
-
-
Save Hironsan/11d6eb0c714869bfb5d2447128a33eef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import os | |
# Load api keys. | |
consumer_key = os.environ.get('CONSUMER_KEY') | |
consumer_secret = os.environ.get('CONSUMER_SECRET') | |
access_token = os.environ.get('ACCESS_TOKEN') | |
access_token_secret = os.environ.get('ACCESS_TOKEN_SECRET') | |
import tweepy | |
# Search tweets. | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
api = tweepy.API(auth) | |
tweets = tweepy.Cursor(api.search, q='地震', lang='ja').items(100) | |
# Write tweets to json file. | |
import json | |
from datetime import datetime | |
filename = 'tweets_{}.jsonl'.format(datetime.now().strftime('%Y%m%d%H%M%S')) | |
with open(filename, 'w') as f: | |
for tweet in tweets: | |
dic = {'text': tweet.text, | |
'date': str(tweet.created_at), | |
'id': tweet.id, | |
# 'geo': tweet.geo, | |
'user_id':tweet.user.id} | |
json.dump(dic, f) | |
f.write('\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment