Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Building WhatsApp bot on Python
Next article icon

Twitter is an American microblogging and social networking service on which users post and interact with messages known as "tweets". In this article we will make aTwitter Bot using Python.

Python as well as Javascript can be used to develop anautomatic Twitter bot that can do many tasks by its own such as:

Requirements

Install Tweepy 

For all this we will need a Python library calledTweepyfor accessing theTwitter API. We can install tweepy in three ways:

1. Using pip command

$ pip install tweepy

2. Clone the GitHub repository of tweepy

$ git clone https://github.com/tweepy/tweepy$ cd tweepy$ pip install

3. Cloning the repository directly

$ pip install git+https://github.com/tweepy/tweepy

Sign up for Twitter Developer Account

Developing the Twitter Bot

Make a filetwitterbot_retweet.py and paste the following code.

Python3
importtweepyfromtimeimportsleepfromcredentialsimport*fromconfigimportQUERY,FOLLOW,LIKE,SLEEP_TIMEauth=tweepy.OAuthHandler(consumer_key,consumer_secret)auth.set_access_token(access_token,access_token_secret)api=tweepy.API(auth)print("Twitter bot which retweets, like tweets and follow users")print("Bot Settings")print("Like Tweets :",LIKE)print("Follow users :",FOLLOW)fortweetintweepy.Cursor(api.search,q=QUERY).items():try:print('\nTweet by: @'+tweet.user.screen_name)tweet.retweet()print('Retweeted the tweet')# Favorite the tweetifLIKE:tweet.favorite()print('Favorited the tweet')# Follow the user who tweeted# check that bot is not already following the userifFOLLOW:ifnottweet.user.following:tweet.user.follow()print('Followed the user')sleep(SLEEP_TIME)excepttweepy.TweepErrorase:print(e.reason)exceptStopIteration:break

Now make another file to specify what should your bot do. Name itconfig.py

Edit the#hashtag according to your choice and the like or follow option to eitherTrue or False.

Python3
# Edit this config.py file as you like# This is hastag which Twitter bot will# search and retweet You can edit this with# any hastag .For example : '# javascript'QUERY='# anything'# Twitter bot setting for liking TweetsLIKE=True# Twitter bot setting for following user who tweetedFOLLOW=True# Twitter bot sleep time settings in seconds.# For example SLEEP_TIME = 300 means 5 minutes.# Please, use large delay if you are running bot# all the time so that your account does not# get banned.SLEEP_TIME=300

Next make a filecredentials.py and paste your access tokens carefully in between the single quotes ' '.

Python3
# This is just a sample file. You need to# edit this file. You need to get these# details from your Twitter app settings.consumer_key=''consumer_secret=''access_token=''access_token_secret=''

Deployment

Run thetwitterbot_retweet.py file from your Command Prompt/Terminal with this command.

$ python twitterbot_retweet.py

And it works!!


Make a Twitter Bot in Python

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp