--Getting a specific user's timeline to analyze Twitter in various ways --Acquired items (tweet ID, created_at, tweets, number of Favs, number of RTs) ――It is a humanities brain studying Python. Please note
--Install Tweepy -Register Twitter API
!/usr/bin/env python3
-*- coding: utf-8 -*-
import tweepy
import csv
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
# Get Tweets
tweet_data = []
for tweet in tweepy.Cursor (api.user_timeline, screen_name = "Enter the name you want to get here", exclude_replies = True) .items ():
tweet_data.append([tweet.id,tweet.created_at,tweet.text.replace('\n',''),tweet.favorite_count,tweet.retweet_count])
# csv output
with open('tweets_20170805.csv', 'w',newline='',encoding='utf-8') as f:
writer = csv.writer(f, lineterminator='\n')
writer.writerow(["id","created_at","text","fav","RT"])
writer.writerows(tweet_data)
pass
――It was very clean. Long live Tweepy! ――I still don't understand the details. ――I want to do natural language processing using this data.
Recommended Posts