[PYTHON] Get lots of your tweets with Tweepy

Here's how to use Tweepy, a Python library, to collect a large number of your tweets.

Premise

--Use Python3 series (Persons of Python2 series should do their best) --Tweepy talks on the assumption that it is already installed. --Twitter API registration has already been registered

code

Tweet_data.py


# -*- coding: utf-8 -*-

import tweepy

#Tweepy settings
CONSUMER_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
CONSUMER_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
ACCESS_TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
ACCESS_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth)

i = 1
with open("Tweet_data.txt", "a+") as tf:
    for status in tweepy.Cursor(api.user_timeline).items():
        try:
            status = str(status.text).replace("\n","")    #Remove line breaks in tweets
            if "RT" in status:  #RT does not write
                pass
            elif "https" in status: #Do not write tweets with images and URLs
                pass
            elif "@" in status: #In the case of rip, remove the ID and write
                status = status[status.find(" ")+1:len(status)] # "@"From" "Get the index and get only the part after that
                tf.write(status+"\n")
                print("Step%d: "%(i) +status)   #View tweets written to a txt file
                i += 1
            else:
                tf.write(status+"\n")
                print("Step%d: "%(i) +status)   #View tweets written to a txt file
                i += 1
        except UnicodeEncodeError: #When I'm running, I suddenly get a UnicodeEncodeError, but it continues
            pass

When this code is executed, it will continue to write to the txt file forever until the first tweet, so if enough tweets are collected, if you interrupt it appropriately with ctrl + c, it will be saved in the txt file.

Created txt file

Succeeded in securing a large amount of my tweets
I want to know the language model network
Ero
After all, Masgomi
How much tweet data should I collect?
Maybe this will give you a Unicode Error
Take care ... If you feel something is wrong, go to the hospital and see it.
Should buy
Is it a little better if you wash it?
The source is my grandma
Well, maybe it swells and it only hurts for a while
If this was a stray dog, it was dead
If you don't disinfect it, you won't die, but it will swell ...
e…! ?? !! ?? !! ?? I haven't disinfected it! ?? !! ?? !! ?? !! ??
It was good
Are you okay…
UnicodeEncodeError: 'cp932' codec can't encode character '\U0001f4a2' in position 28: illegal multibyte sequence
I'm really worried ...
I'm worried about what I'm worried about even if I'm told it's okay
I'm really worried
worry
Is it really okay
No no no no no
variable.find[x:y]Seems to go
I want to extract only after the white space
For example @JUN_NETWORKS When there was a rip, I want to erase only the ID part and take out only the text like this rip.
@Is there a way to remove only whitespace characters from the string?
I said that it was pretty good, but somewhere I got the ID of Lip, so I have to erase it
It doesn't look okay at all
All right…?
Be bitten too much by a northern dog ...

Well, you can create a file like this.

If you have any questions, please leave a comment or send me a rip on my Twitter and I will answer.

Recommended Posts

Get lots of your tweets with Tweepy
Get Tweets with Tweepy
Get all live tweets of professional baseball
Get lots of your tweets with Tweepy
Get replies to specific tweets with tweepy
Get tweets containing keywords using Python Tweepy
Exclude tweets containing URLs with tweepy [Python]
Sentiment analysis of tweets with deep learning
Get all live tweets of professional baseball
Get a list of packages installed in your current environment with python
How to selectively delete past tweets with Tweepy
Get a lot of Twitter tweets at once
[Python] Get rid of dating with regular expressions
Get a list of IAM users with Boto3
Get CPU information of Raspberry Pi with Python
Flow of creating your own package with setup.py with python
Get media timeline images and videos with Python + Tweepy
Get your current location with a USB GPS receiver
Get the sum of each of multiple columns with awk
Define your own distance function with k-means of scikit-learn
Try to get the contents of Word with Golang
Get twitter tweets, followers, followers, likes, with urllib and beautifulSoup
Get the operation status of JR West with Python
Get tweets with arbitrary keywords using Twitter's Streaming API
Collecting tweets with Python
Get started with MicroPython
Get date with python
Get started with Mezzanine
Get the id of a GPU with low memory usage
Get UNIXTIME at the beginning of today with a command
Run the intellisense of your own python library with VScode.
Your URL didn't respond with the value of the challenge parameter.
Get rid of dirty data with Python and regular expressions
Get stock articles of infrastructure engineer yuta with Qiita API
Visualize the flow rate of tweets with Diamond + Graphite + Grafana
Sample of HTTP GET and JSON parsing with python of pepper
How to get an overview of your data in Pandas
Get the host name of the host PC with Docker on Linux
Get the source of the page to load infinitely with python.