We have prepared a list containing screen_names (@ ----) for multiple Twitter accounts.
(Reference) [python] Create a list of users who tweeted including the character string in the list https://qiita.com/Yoocie/items/5ba57645d38ee5203ef7
This time, we will get the timeline of the users included in the created list and save it as a txt file. Make sure that one txt file is created for each timeline.
The maximum number of tweets that can be acquired per timeline is 3,200, and RT is not acquired.
The prepared list looks like this. (Actually, the string will be the screen_name for each user.)
users.py
users=['---','vvv','^^^',...(Omission)...,'+++',')))']
get_timeline.py
import tweepy
import config #In the same directory'config.py'There is a file called.
'''
config.py
CK = "*****" #CONSUMER_KEY
CS = "*****" #CONSUMER_SECRET
AT = "*****" #ACCESS_TOKEN
ATS = "*****" #ACCESS_TOKEN_SECRET
'''
CK = config.CK
CS = config.CS
AT = config.AT
ATS = config.ATS
#OAuth authentication
auth = tweepy.OAuthHandler(CK, CS)
auth.set_access_token(AT, ATS)
api = tweepy.API(auth)
#Definition of a function that gets the timeline for one user and saves it as a text file
def get_timeline(name,i):
#Get up to 3200 tweets per person
pages = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
data = []
for page in pages:
results = api.user_timeline(screen_name=name,\
include_rts=False,\#You won't get retweets.
count=200,\#Get tweets up to 200 each.
page=page)
for result in results:
data.append(result.text)
line=''.join(data)
#Create and save a text file containing the contents of the timeline
#The name of the text file is'20191210_user3_^^^.text'Format like
with open('20191210_user'+str(i)+'_'+name+'.txt', 'wt') as f:
f.write(line)
Use the function defined above. Here we get the timeline for the first 6 accounts in the user list.
for i in range(6):
get_timeline(users[i],i)
macOS Catalina Jupiter notebook
Recommended Posts