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.
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
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.
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
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.
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)
How to use Twitter's Streaming API in Python [Tweepy] --NIXOBLOG See this article.
This time it's over. If you have any other concerns, please comment, rip on Twitter, DM, etc.
Recommended Posts