[Tweepy] Re: Twitter Bot development life starting from zero # 1 [python]

at first

We will proceed on the assumption that the installation of tweepy has already been completed.

Development environment

python-3.8.5 tweepy-3.9.0 editor-Atom

For programming beginners

Even though the Lord is a beginner, he has some experience. If you are a complete beginner, please read other articles, acquire basic knowledge, and then come back again.

Main story

Then it is the main story.

code

Required code

import system


import tweepy

Token system


CONSUMER_KEY = "Enter the corresponding character string here"
CONSUMER_SECRET = ""
ACCESS_KEY = ""
ACCESS_SECRET = ""

Other


auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)

Code to actually write

This time, I will write the code to print 10 tweets excluding replies and retweets.

The entire


a = 0
public_tweets = api.home_timeline()
for tweet in public_tweets:
    if a < 10:
        if tweet.text[:2] == 'RT' or tweet.text[:1] == '@':
            pass
        else:
            print(' ')
            print('-----------------')
            print(' ')
            print(tweet.text)
            a += 1
Commentary

The first line


a = 0

Definition required to finish after 10 operations.

2nd line


public_tweets = api.home_timeline()

I'm digging into public_tweets to make it easier to call ʻapi.home_timeline ()`.

3rd line


for tweet in public_tweets:

It is a guy who puts the acquired tweet object in a variable called tweet.

4th line


if a < 10:

Check to suppress the operation with 10 times. The first line is required.

5,6th line


if tweet.text[:2] == 'RT' or tweet.text[:1] == '@':
    pass

The one to ignore if it is a retweet or reply tweet. Basically, if you print a retweet or reply tweet with tweet.text RT @ user ID message content Or @User ID message content It's like that, RT or @ is added at the beginning, so it is for judging it.

7~12th line


else:
    print(' ')
    print('-----------------')
    print(' ')
    print(tweet.text)
    a += 1

ʻElse:works if the conditions on lines 5 and 6 are not met. The top three lines of print do not have to be the ones to make it easier to distinguish the acquired tweet contents. print (tweet.text) is the one that prints the content of the tweet. ʻA + = 1 is the one that adds 1 to the value of the variable called a. Required code to check how many times it worked.

By the way, if you operate this,


-----------------

Message content 1(I will hide it just in case)

-----------------

Message content 2(I will hide it just in case)

-----------------

Message content 3(I will hide it just in case)

-----------------

Message content 4(I will hide it just in case)

-----------------

Message content 5(I will hide it just in case)

-----------------

Message content 6(I will hide it just in case)

-----------------

Message content 7(I will hide it just in case)

-----------------

Message content 8(I will hide it just in case)

-----------------

Message content 9(I will hide it just in case)

-----------------

Message content 10(I will hide it just in case)

It will be.

next

Next, we will add the function to display the user name. The execution result is

-------

username
|
Tweet content

-------
.
.
.

I will try to be. Let's write the code immediately. I don't think it's that difficult because I just add a little. High Code Pon

a = 0
public_tweets = api.home_timeline()
for tweet in public_tweets:
    if a < 10:
        if tweet.text[:2] == 'RT' or tweet.text[:1] == '@':
            pass
        else:
            print(' ')
            print('-----------------')
            print(' ')
            print(tweet.user.name)
            print('|')
            print(tweet.text)
            a += 1

Up to the 7th line is the same, so hit it and pop

print(' ')
print('-----------------')
print(' ')
print(tweet.user.name)
print('|')
print(tweet.text)
a += 1

The only thing that has changed is the 4th and 5th lines! And just add! No deletion! I don't think this needs to be explained in particular, so use the path. The execution result looks like this

Execution result



-----------------

Username 1(Not a user ID)
|
Message content 1(I will hide it just in case)

-----------------

Username 2(Not a user ID)
|
Message content 2(I will hide it just in case)

-----------------

Username 3(Not a user ID)
|
Message content 3(I will hide it just in case)

-----------------

Username 4(Not a user ID)
|
Message content 4(I will hide it just in case)

-----------------

Username 5(Not a user ID)
|
Message content 5(I will hide it just in case)

-----------------

Username 6(Not a user ID)
|
Message content 6(I will hide it just in case)

-----------------

Username 7(Not a user ID)
|
Message content 7(I will hide it just in case)

that? Can you get only 7 tweets? ?? That's right. This time, I got only 7 tweets. One possible cause,

Before we got 10 tweets that met this condition, the number of tweets that could be acquired was limited in the first place.

Maybe this is the cause. I think the result will change if I do it a few more times. ~~ I won't do it because it's annoying. ~~

Last Ooo! !! !!

The last challenge is to write your Twitter ID to the right of your username! !! I would like to finish this time by doing only this. Then immediately. This time I will write from thinking

First of all, I want to write the user name and Twitter ID in the same column, so I have to decide some variables. (Well, there is a way to do it without using variables) This time we will use a variable called name.

name = tweet.user.name

As I did earlier, this will get the username. And next. Get the UserID. I don't know where to get it, so when I'm searching for Object ... There was something like that called screen_name. When I try searching on Twitter ...

Yes, it worked. (There is no image because it is a user ID of a stranger) So, I found out that I can get a Twitter ID by using this. Then it's easy. Combine these guys ...

name = tweet.user.name + ':@' + tweet.user.screen_name

Maybe this will work! The result of trying it out ...

Execution result



-----------------

username:@TwitterID
|
Message content 1(I will hide it just in case)

-----------------

username:@TwitterID
|
Message content 2(I will hide it just in case)

-----------------

username:@TwitterID
|
Message content 3(I will hide it just in case)

-----------------

username:@TwitterID
|
Message content 4(I will hide it just in case)

-----------------

username:@TwitterID
|
Message content 5(I will hide it just in case)

-----------------

username:@TwitterID
|
Message content 6(I will hide it just in case)

-----------------

username:@TwitterID
|
Message content 7(I will hide it just in case)

-----------------

username:@TwitterID
|
Message content 8(I will hide it just in case)

-----------------

username:@TwitterID
|
Message content 9(I will hide it just in case)

-----------------

username:@TwitterID
|
Message content 10(I will hide it just in case)

Yes, it worked. It's Sorya likely.

Finally

Well, this time I would like to end with this. Next time, I would like to update it as soon as I can think of what I want to make.

Writing Qiita improves typing stability and I feel that writing skills will improve, so it's good. You can get to know yourself even more by teaching others. Isn't it three birds with one stone?

Please write an article by all means! Then, noshi

Re: Twitter Bot Development Life Series Starting from Zero

\ # 1 This article \ # 2 * Preparing *

・ As soon as this series is updated, we will update it as needed.

Recommended Posts

[Tweepy] Re: Twitter Bot development life starting from zero # 1 [python]
Re: Competitive Programming Life Starting from Zero Chapter 1.2 "Python of Tears"
Re: Competitive Programming Life Starting from Zero Chapter 1.3 "Sedo Tea"
Tweet from python with Twitter Developer + Tweepy
Python explosive environment construction starting from zero (Mac)
Re: Competitive programming life starting from zero Chapter 1 1 "Only C ++ can be used"
[Python] Django Source Code Reading View Starting from Zero ①
ChIP-seq analysis starting from zero
[For beginners] Re: Genetic algorithm starting from zero [Artificial intelligence]
Re: Life in Heroku starting from scratch with Flask ~ PhantomJS to Heroku ~
[Lambda] [Python] Post to Twitter from Lambda!
Code wars kata starting from zero
Get data from Twitter using Tweepy
Re: Life in Heroku starting from scratch with Flask ~ Selenium & PhantomJS & Beautifulsoup ~
Re: Heroku life begin with Flask from zero - Environment and Hello world -
Collecting information from Twitter with Python (Twitter API)
"Python AI programming" starting from 0 for windows
Business efficiency starting from scratch with Python
Let's make a Twitter Bot with Python!