How to use Tweepy ~ Part 1 ~ [Getting Tweet]

Introduction

I will summarize how to use tweepy with reference to the official document.

Hello Tweepy ! Assign import and the acquired API key.

import tweepy

consumer_key = '*********************************'
consumer_secret = '*************************************'
access_token = '*******************************************'
access_token_secret = '****************************************'

Tweepy uses OAuthHandler to authenticate. As shown in the example below, ʻauth = tweepy.OAuthHandler (consumer_key, consumer_secret), ʻauth.set_access_token (access_token, access_token_secret), and then ʻapi = tweepy.API (auth)` Use OAuth for authentication in the request.

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

public_tweets = api.home_timeline()
for tweet in public_tweets:
    print('-------------------------')
    print(tweet.text)

When you do this, you should see a timeline. Hello Tweepy !!

Calling an API method with ʻapi = tweepy.API (auth)` returns a Tweepy model class instance. This contains the data returned by Twitter, which we will use below.

Search for tweets

Tweets containing the specified word

api = tweepy.API(auth)

for tweet in tweepy.Cursor(api.search, q='Smash Bra').items(10):
    print(tweet)

When executed, you can get the latest 10 tweets including "Smash Bros.". The value returned by this tweepy.Cursor is a Status object. As you can see by looking at the printed tweet, you can get various information by doing something like tweet. ○○.

For example, tweet.text will display the tweet content, tweet.user will allow you to access the tweeted user information, and tweet.user.name will give you the user name. You can fetch it.

When these are applied, the user name is acquired only when the user himself tweeted, not RT, among the tweets containing "Smash Bros.". You can do something like

[tweet.user.name for tweet in tweepy.Cursor(api.search, q='Smash Bra').items(10) if list(tweet.text)[:2] != ['R', 'T']]

#I'll leave the name down for the time being.
>> ['*********',
    '***********',
    '*******************',
    '********************',
    '**********']

Tweet of a specific user

By changing the argument of tweepy.Cursor as follows, you can get the tweets of the account with the specified id.

#Exclude RT and reply in if statement
[tweet.text for tweet in tweepy.Cursor(api.user_timeline, id="Qiita").items(10) if (list(tweet.text)[:2]!=['R', 'T']) & (list(tweet.text)[0]!='@')]

>> ['3500 likes!|Practical English comment collection used on GitHub by@shikichee https://t.co/njAmOmPECK',
    '1000 Contribution! | @ruccho_vector https://t.co/691RY2XE7U',
    '600 likes!|Hands-on to create a simple web chat app in 1 hour with Firebase@taketakekaho https://t.co/r0agD6Z6Qf',
    '500 likes!|Three tips to avoid shell script traps by@tnacigam https://t.co/rKO7tBQYii',
    '1300 likes!|Implementation clean architecture by@nrslib https://t.co/ZCMaz4ges6',
    'Regarding the article that we shared only on December 25, 2019, we have described the reason for the limited sharing and the background of our response on this blog.\n\nhttps://t.co/SUqVaUKPbh',
    '300 likes!|Aiming for a safe CSS ~ What we can do for a peaceful world ~ https://t.co/bww0EpoNDV',
    '400 likes!|Icon that seems to be out of work 10 years later by@otktko https://t.co/4sFpzK1zgu',
    '700 likes!|Publish "question items" and "intentions" in engineer recruitment interviews by@ka_me_sen_nin https://t.co/GvM7RzC4m5',
    '1000 Contribution! | @phanect_ja https://t.co/glKfw9y6vW']

Get page by page

If you try to get 3 pages, you can see that 20 tweets per page can be obtained because the length of each list is 20.

#By default, your tweets are retrieved.
for page in tweepy.Cursor(api.user_timeline).pages(3):
    #page is a list of status.
    print(len(page))

>> 20
   20
   20

Now let's see if there is a difference in execution speed between tweepy.Cursor (). Pages () and tweepy.Cursor (). Items ().

%%time
for page in tweepy.Cursor(api.user_timeline).pages(3):
    # get 60 tweet by pages
    print(page)

>> CPU times: user 67.5 ms, sys: 15.9 ms, total: 83.4 ms
Wall time: 490 ms
%%time
for tweet in tweepy.Cursor(api.user_timeline).items(60):
    # get 60 tweet by items
    print(tweet)

>> CPU times: user 64 ms, sys: 6.8 ms, total: 70.8 ms
Wall time: 471 ms

The latter seems to be a little faster.

in conclusion

Now you can get various tweets. It may be fun to write code that performs automatic favorites and automatic follow after filtering users and tweets by making full use of if statements.

Next time, I will continue to use the API to perform operations such as follow and like. -> Sequel: How to use Tweepy ~ Part 2 ~ [Follow, like, etc.]

Recommended Posts

How to use Tweepy ~ Part 1 ~ [Getting Tweet]
How to use Tweepy ~ Part 2 ~ [Follow, like, etc.]
How to use cybozu.com developer network (Part 2)
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use shogun
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to use Qt Designer
How to use search sorted
python3: How to use bottle (2)
Understand how to use django-filter
How to use the generator
How to use FastAPI ③ OpenAPI
How to use Python argparse
How to use IPython Notebook
How to use Pandas Rolling
[Note] How to use virtualenv
How to use redis-py Dictionaries
Python: How to use pydub
[Python] How to use checkio
[Go] How to use "... (3 periods)"
How to use Django's GeoIp2
[Python] How to use input ()
How to use the decorator
[Introduction] How to use open3d
How to use Python lambda
How to use Jupyter Notebook
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Google Colaboratory
How to use Python bytes