Python: Tweepy library

in utopian-io •  7 years ago  (edited)

Python: Tweepy library for Twitter

Create twitter bot using Python and Tweepy. Twitter is a popular social media website and has many bots. We will do our own Twitter bot in a simple way using Python and Tweepy libraries.

Tweepy, easy to use and open source. It’s a Python library that provides acces to the Twitter API.

  • You will learn use Tweepy library
  • You will learn tweet with code
  • You will learn get data from Twitter

Requirements

The easiest way to install the latest version is by using pip/easy_install to pull it from PyPI:

 pip install tweepy

You may also use Git to clone the repository from Github and install it manually:

git clone https://github.com/tweepy/tweepy.git
cd tweepy
python setup.py install

Python 2.6 and 2.7, 3.3, 3.4, 3.5 & 3.6 are supported.

Difficulty

  • Basic, for everyone

Tweepy

Create a Twitter application

First, you need to have a Twitter account. Then we can start to create the application. We enter the this address get the work.

chrome_2017-12-05_16-58-25.png

Fill in the necessary areas and proceed.** PS: To be able to create an application, your phone number needs to be defined on Twitter.**

chrome_2017-12-05_17-03-35.png

We come to the Keys and Access Token section:
1.Consumer Key (API Key)
2.Consumer Secret (API Secret)
3.Access Token
4.Access Token Secret we take note of the information.

Download Tweepy

Now that we've created our application, we can start calling the Tweepy library and typing the Twitter bot.

-open command line
-pip install tweepy

Working with Tweepy

 import tweepy   ##we import the tweepy library

-writing our keys
consumer_key = ‘yours consumer_key’
consumer_secret = ‘yours consumer_secret’
access_token = ‘yours access_token’
access_token_secret = yours access_token_secret’

-we connect with ours keys
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

Hello Tweepy

If you are new to Tweepy, this is the place to begin. The goal of this tutorial is to get you set-up and rolling with Tweepy. We won’t go into too much detail here, just some important basics.

import tweepy
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 (tweet.text)

We're connected and we can start to have fun. We use the following command for tweeting:

api.update_status(status="")
api.update_status(status=”this is a sample tweet using tweepy with python”)

Getting information from Twitter

We can easily do this using the following commands:

user = api.get_user('avnigenc')

print("Name:", user.name)
print("User id: ", user.id_str)
print("Description: ", user.description)
print("Location:",user.location)
print("Time zone: ", user.time_zone)
print("Number of Following:",user.friends_count)
print("Number of Followers:",user.followers_count)
print("Number of tweets: ", str(user.statuses_count))

Output:

Pagination

Iterate through all of the authenticated user's friends

 for friend in tweepy.Cursor(api.friends).items():
         process_friend(friend)

FollowAll

This snippet will follow every follower of the authenticated user.

for follower in tweepy.Cursor(api.followers).items():
    follower.follow()

Don’t forget to visit Tweepy Github for more of an introduction to the Tweepy library!



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

Hey @manishmike10, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

edited