[python] Get Twitter timeline for multiple users

background purpose

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.

User list

The prepared list looks like this. (Actually, the string will be the screen_name for each user.)

users.py


users=['---','vvv','^^^',...(Omission)...,'+++',')))']

Creating a function (getting a timeline, creating a text file)

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 of functions

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)

environment

macOS Catalina Jupiter notebook

Recommended Posts

[python] Get Twitter timeline for multiple users
Get Twitter timeline with python
Modern Python for intermediate users
BigQuery integration for Python users
Post multiple Twitter images with python
Process multiple lists with for in Python
Get a token for conoha in python
Get images from specific users on Twitter
R code compatible sheet for Python users
2016-10-30 else for Python3> for:
python [for myself]
Get a ticket for a theme park with python
twitter on python3
Get multiple maximum keys in Python dictionary type
Get Twitter Trends
Get note information using Evernote SDK for Python 3
Get information on the 100 most influential tech Twitter users in the world with python.
Get Twitter userData
Get media timeline images and videos with Python + Tweepy
Python text reading for multiple lines and one line
[Python] This is easy! Search for tweets on Twitter
Python: Get a list of methods for an object
About Python for loops
[Python] Get environment variables
Python basics ② for statement
[Python] Create multiple directories
[Python] Get Qiita trends
About Python, for ~ (range)
Refactoring tools for Python
python for android Toolchain
[Python3] Get date diff
Get date in Python
Get date with python
Search Twitter using Python
python get current time
OpenCV for Python beginners
Change Maya Python Timeline
Install Python (for Windows)
[Python] for statement error
Python environment for projects
[python] Decompose the acquired Twitter timeline into morphemes with MeCab
A simple way to avoid multiple for loops in Python
Get Python list elements with multiple indexes (number) Simple method
How to define multiple variables in a python for statement
Create a Twitter BOT with the GoogleAppEngine SDK for Python
Get data from analytics API with Google API Client for python