Twitter API Python
With Python, we will see how to create sample applications using the Twitter API. For this, we need to create an application on Twitter first. https://dev.twitter.com/apps/new you can create an application from the address. Once you have created the application, you will see a window like this.
Here consumer key, Consumer secret, access token, access token Secret values we will need. If you do not have values, create your access token by clicking on the Create my access token button under your access token title. We will use the Twython module to use the Twitter API. If twython is not installed on your computer, you can install it by typing the following commands at the terminal.
How to Install Twitter API Python
1 2 3 | pip install twython easy_install twython |
In order to use the Twitter API, we have to make the following definitions:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #!/usr/bin/env python # -*- coding: utf-8 -*- from twython import Twython CONSUMER_KEY = '***' CONSUMER_SEcRET = '***' ACCESS_TOKEN = "***" ACCESS_TOKEN_SECRET = "***" twitter = Twython(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET) |
thanks to the twython Module, we are connected to Twitter. You must have your own values. Now if you want to first
1 | update_status() |
let’s tweet using the function. We add the following line to the above codes.
1 | twitter.update_status(status='#python tweet with open source projects') |
We tweeted successfuly.
Note: if the application you created on the twitterda does not have write permission, you will encounter the error. By default, only read permission is granted when the first application is created.
A large number of followers of a user followed by the number of people we can reach when you take the tweet such as the number of values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | ##Following users user = twitter.get_user_timeline() print user[0]['user']['friends_count'] 138 ##Follower user user = twitter.get_user_timeline() print user[0]['user']['followers_count'] 82 ##tweet count user = twitter.get_user_timeline() print user[0]['user']['statuses_count'] 969 ##username user = twitter.get_user_timeline() print user[0]['user']['screen_name'] _true_false |
We can do this in another user by giving us the user name.
1 2 3 | user = twitter.get_user_timeline(screen_name='sourceprojects') print user[0]['user']['followers_count'] 206 |
Now we want a new one by pulling the last tweets of a user. let’s make a simple application to transfer to the HTML page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #!/usr/bin/env python # -*- coding: utf-8 -*- from twython import Twython APP_KEY = '***' APP_SECRET = '***' ACCESS_TOKEN = "***" ACCESS_TOKEN_SECRET = "***" twitter = Twython(APP_KEY, APP_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET) user_tweets = twitter.get_user_timeline(screen_name='_true_false',include_rts=True) tweets = "" for tweet in user_tweets: tweet = Twython.html_for_tweet(tweet) tweets += '<li>'+tweet+'</li>' html = """ <link rel="stylesheet" href="https://abs.twimg.com/a/1387359134/t1/css/t1_core.bundle.css"> <meta charset="UTF-8"> <div class="tweet"><ul>"""+ tweets +"""</ul></div> """ create_html = open("tweets.html","w") create_html.write(html.encode('utf-8').strip()) create_html.close() |
The screen_name=’_true_false’ parameter specifies the name of the user whose tweets are to be taken. You can also run the program in your current directory where the last tweets are located tweets.the HTML file will be created.
More information https://twython.readthedocs.org/en/latest you can find it on the page.
email marketing for small business. https://www.kalogroup.com.au/