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.
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.
>> ['*********',
'***********',
'*******************',
'********************',
'**********']
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']
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.
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