Various of Tweepy. Ma ♡ and ♡ me ♡

This time, I'm writing this article because I want people who are just starting to study Python to try Twitter development. If you know how to use Tweepy, you can try creating a Twitter management app or easily create a Twitter Bot in Python.

First preparation

To use Tweepy, you need a Twitter development account. However, it is not that difficult, and you can set your usual Twitter account as a developer account like + α. Of course, you can still use it as a normal account without any substitute. Currently, the version has changed from 1.1 to 2, and the UI has changed significantly, but the functions themselves are almost the same, so please refer to this article and apply. It may be approved in minutes or days. It's a luck game.

If you don't understand, please DM or mention me on Twitter. @NIIKUUN

Install Tweepy

Since it is published on pypi.org, install it with PIP.

pip install tweepy

If you get an error, try `` `pip3 install tweepy``` or run it with administrator privileges.

Try tweeting with Tweepy

Tokens etc. are required when executing tweets etc. Access Twitter Developers Dashboard with the developer account you applied for earlier and create an application. Once created, you will be able to view or generate consumer keys and access tokens.

tweet.py


import tweepy

#The one who writes every time from here
CK = "Consumer Key here"
CS = "Here is the Consumer Secret"
AT = "Access Token here"
ATS = "Access Token Secret here"

auth = tweepy.OAuthHandler(CK, CS)
auth.set_access_token(AT, ATS)
api = tweepy.API(auth)
#The one who writes so far every time

api.update_status("Yaho!\Tweets from nTweepy!")

By doing this, you can tweet ** "Yahho! Tweet from Tweepy!" ** On Twitter.

It's easy. I think that you can create various BOTs with just this.

By the way, the contents of the one you write every time are always written when using Tweepy, so it may be better to remember

Try to get the timeline

This time, I will try to get the 10 timelines from the top.

timeline.py


import tweepy

CK = "Consumer Key here"
CS = "Here is the Consumer Secret"
AT = "Access Token here"
ATS = "Access Token Secret here"
auth = tweepy.OAuthHandler(CK, CS)
auth.set_access_token(AT, ATS)
api = tweepy.API(auth)


home_timeline = api.home_timeline(count=10) #If you change the value of count here, the number to get will also change.

for status in home_timeline:
    print("#----------#") #Decorated for easy viewing
    print(status.user.name) #Output user name
    print(status.text) #Output tweet content

By doing this, you can get the 10 timelines from the top.

Like and follow the top 10 on the timeline.

Next, I'll like and follow you. Like from the tweet ID and follow from the user's ID.

Likes and followers may cause errors, so exception handling is applied.

favfol.py


import tweepy

CK = "Consumer Key here"
CS = "Here is the Consumer Secret"
AT = "Access Token here"
ATS = "Access Token Secret here"
auth = tweepy.OAuthHandler(CK, CS)
auth.set_access_token(AT, ATS)
api = tweepy.API(auth)

home_timeline = api.home_timeline(count=1) #If you change the value of count here, the number to get will also change.

for status in home_timeline:
    print("#----------#") #Decorated for easy viewing
    print(status.user.name) #Output user name
    print(status.text) #Output tweet content
    try:
        api.create_favorite(status.id) #Like the tweet
        print("Liked successfully")
    except Exception as e:
        print("I failed to like")
        print(e)
    try:
        api.create_friendship(status.user.id) #Follow the tweeter
        print("I succeeded in following")
    except Exception as e:
        print("Failed to follow")
        print(e)

Real-time search like TweetDeck

How to use Twitter's Streaming API in Python [Tweepy] --NIXOBLOG See this article.

end

This time it's over. If you have any other concerns, please comment, rip on Twitter, DM, etc.

Recommended Posts

Various of Tweepy. Ma ♡ and ♡ me ♡
[Python] Various combinations of strings and values
Various processing of Python
With me, cp, and Subprocess
Problems of liars and honesty
Various class methods and static methods
Mechanism of pyenv and virtualenv
Pre-processing and post-processing of pytest
Combination of recursion and generator
Combination of anyenv and direnv
Explanation and implementation of SocialFoceModel
Various import methods of Mnist
About various encodings of Python 3
Differentiation of sort and generalization of sort
Coexistence of pyenv and autojump
Use and integration of "Shodan"
Problems of liars and honesty
Occurrence and resolution of tensorflow.python.framework.errors_impl.FailedPreconditionError
Comparison of Apex and Lamvery
Source installation and installation of Python
Introduction and tips of mlflow.Tracking
py, shebang, venv and me
With me, NER and Flair