Collecting information from Twitter with Python (Twitter API)

Access Twitter information from Python

To access Twitter information from Python, use OAuth authentication and Exchange information through the Twitter API.

Below is a description of them and sample code.

Creating an authentication key for your Twitter application

Twitter uses an authentication method called OAuth authentication as the authentication method.

The details of how to create these are described on the following page, so refer to here to create four authentication keys. [Create twitter application](http://website-planner.com/ Create twitter application (consumer-key, consumer-secret, access-token, access-token-secret /)

Python library installation for OAuth authentication

This time, access is performed using the OAuth authentication library for Python called requests-oauthlib.

Execute the following command under the twi-py environment.

(twi-py)$ pip install requests requests_oauthlib

Operation check.

(twi-py)$ python
>>> from requests_oauthlib import OAuth1Session
>>>

OK if no error occurs.

Twitter API Basically, the following API is used to acquire Twitter data and send information from the program. https://dev.twitter.com/rest/public

The purpose of this time is to search with the specified keyword and get the necessary information. Use GET search / tweets. https://dev.twitter.com/rest/public/search

Sample code using Twitter search API

tweet_crawler.py Sample code of a program that searches with the hashtag of #python and outputs the result. Anyone who can read and write Python will know what they are doing in each function.

Basically, if you play around with url, params, oath.get () / oath.post (), Let's play with it because it can be operated using other Twitter APIs.

#!/usr/bin/env python                                                                                                                                             
# -*- coding:utf-8 -*-                                                                                                                                            

from requests_oauthlib import OAuth1Session
import json

### Constants                                                                                                                                                     
oath_key_dict = {
    "consumer_key": "xxxxxxxxxxxxxxxxxxxx",
    "consumer_secret": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "access_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "access_token_secret": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

### Functions                                                                                                                                                     
def main():
    tweets = tweet_search("#python", oath_key_dict)
    for tweet in tweets["statuses"]:
	tweet_id = tweet[u'id_str']
        text = tweet[u'text']
        created_at = tweet[u'created_at']
        user_id = tweet[u'user'][u'id_str']
	user_description = tweet[u'user'][u'description']
	screen_name = tweet[u'user'][u'screen_name']
	user_name = tweet[u'user'][u'name']
	print "tweet_id:", tweet_id
	print "text:", text
	print "created_at:", created_at
	print "user_id:", user_id
	print "user_desc:", user_description
	print "screen_name:", screen_name
	print "user_name:", user_name
    return


def create_oath_session(oath_key_dict):
    oath = OAuth1Session(
	oath_key_dict["consumer_key"],
	oath_key_dict["consumer_secret"],
	oath_key_dict["access_token"],
	oath_key_dict["access_token_secret"]
	)
    return oath

def tweet_search(search_word, oath_key_dict):
    url = "https://api.twitter.com/1.1/search/tweets.json?"
    params = {
        "q": unicode(search_word),
        "lang": "ja",
        "result_type": "recent",
        "count": "15"
        }
    oath = create_oath_session(oath_key_dict)
    responce = oath.get(url, params = params)
    if responce.status_code != 200:
        print "Error code: %d" %(responce.status_code)
        return None
    tweets = json.loads(responce.text)
    return tweets

### Execute                                                                                                                                                       
if __name__ == "__main__":
    main()

Output example

(twi-py)$ python tweet_crawler.py
tweet_id: 531312631831732225
text:Shannon Lab holds regular python study sessions at the local Hachioji. Beginners of programming can also participate. Search for "Python study group Hachioji". ATND\
You can register from.#python
created_at: Sun Nov 09 05:09:37 +0000 2014
user_id: 426868502
user_desc:With creativity, change people and the world. Shannon Lab creates an innovative world through the fusion of mathematics, artificial intelligence and art. Programming study sessions and Android app products\
Tweet the information. Currently, we are planning a study session to make Android applications with Python. We are looking for internship engineers.
screen_name: shannon_lab
user_name: Shannon Lab
tweet_id: 531253415548944385
text:Looking at it like this, hands-on for beginners may be quite meaningful.
The story of a designer who met Python#PyLadiesTokyo by @uniq #python http://t.co/rZMEruT4yf @From SlideShare
created_at: Sun Nov 09 01:14:19 +0000 2014
user_id: 337419400
user_desc:Nichinichi engineer
screen_name: icoxfog417
user_name:Pixie
tweet_id: 531247311288946688
text:The story of a designer who met Python#PyLadiesTokyo by @uniq #python http://t.co/MwMFS5IFQ1 null
created_at: Sun Nov 09 00:50:03 +0000 2014
user_id: 1497018468
user_desc: Software Engineer
.NET/C#/HTML5/CSS3/JavaScript/jQuery/Perl/Ruby/Python
screen_name: teruru2013
user_name:Teru
... 

Talking about Twitter API restrictions

https://dev.twitter.com/rest/public If you read the explanation of each API, you will find the following description.

Requests / 15-min window (user auth)  180
Requests / 15-min window (app auth)   450

This is the maximum number of times you can get information using the API per 15 minutes. The default is 180 times / 15min in this case.

Access from other than one Access token and Access token secret, If you use app auth, it will be 450 times / 15min.

Details of app auth are described at the following URL on the official page. https://dev.twitter.com/oauth/application-only

As soon as you collect information, you will get caught, so you need to consider a workaround. Basically, it is best to hold down the number of times that you do not hit the upper limit, If you need more search results than the limit in a short time, get multiple Twitter ids and When the response status reaches the upper limit of the key required for OAuth authentication There is a way to switch to the next key and continue acquisition (internally, it is a multiple Twitter application configuration). However, be aware that if you mass-produce your accounts too much, the risk of your accounts being frozen increases.

Recommended Posts

Collecting information from Twitter with Python (Twitter API)
Collecting information from Twitter with Python (Environment construction)
Collecting information from Twitter with Python (morphological analysis with MeCab)
Collecting information from Twitter with Python (MySQL and Python work together)
Use Twitter API with Python
[python] Read information with Redmine API
Python: Extract file information from shared drive with Google Drive API
Tweet from python with Twitter Developer + Tweepy
Post from another account with Twitter API
[Python] Get Python package information with PyPI API
Collecting tweets with Python
Use Trello API with python
Twitter graphing memo with Python
Get Twitter timeline with python
Information obtained from tweet_id (Python)
Get schedule from Garoon SOAP API with Python + Zeep
Get information with zabbix api
Web API with Python + Falcon
Crawling with Python and Twitter API 1-Simple search function
Play RocketChat with API / Python
Support yourself with Twitter API
Call the API with python3.
Search twitter tweets with python
Use subsonic API with python3
[Python] Get user information and article information with Qiita API
[Basics of data science] Collecting data from RSS with python
With skype, notify with skype from python!
Successful update_with_media with twitter API
Use e-Stat API from Python
Get Alembic information with Python
Try hitting the Twitter API quickly and easily with Python
I tried follow management with Twitter API and Python (easy)
Streamline information gathering with the Twitter API and Slack bots
Get data from analytics API with Google API Client for python
Call C from Python with DragonFFI
Create Awaitable with Python / C API
Using Rstan from Python with PypeR
Get reviews with python googlemap api
Install Python from source with Ansible
Create folders from '01' to '12' with python
Run Rotrics DexArm with python API
Specifying the date with the Twitter API
[Lambda] [Python] Post to Twitter from Lambda!
Quine Post with Qiita API (Python)
Hit the Etherpad-lite API with Python
Post multiple Twitter images with python
Run Aprili from Python with Orange
Use kabu StationĀ® API from Python
Use the Flickr API from Python
Get upcoming weather from python weather api
Call python from nim with Nimpy
Easily post to twitter with Python 3
Run Ansible from Python using API
Read fbx from python with cinema4d
Access the Twitter API in Python
Get weather information with Python & scraping
Use Google Analytics API from Python
Handle SOAP API from Python (Zeep)
[Memo] Tweet on twitter with python
[Python] Scraping lens information from Kakaku.com
Crawling with Python and Twitter API 2-Implementation of user search function