8
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python3でTwitter API

Last updated at Posted at 2018-06-26

1.準備##

下記のサイトを参考にしてください。
https://cre8cre8.com/python/twitter-api.htm

2.APIの認証設定##

設定ファイル

config.py
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""

それぞれに1.準備で作成した値を入れる。

3.Code1##

まずは自分ツイートした内容を任意の数表示。

show_twi.py
# -*- coding:utf-8 -*-
import json, config
from requests_oauthlib import OAuth1Session
CK = config.CONSUMER_KEY
CS = config.CONSUMER_SECRET
AT = config.ACCESS_TOKEN
ATS = config.ACCESS_TOKEN_SECRET
twitter = OAuth1Session(CK,CS,AT,ATS)
url = "https://api.twitter.com/1.1/statuses/user_timeline.json"
num = input(str("何件のツイートを表示しますか?"))
params ={'count' : num}
req = twitter.get(url, params = params)

if req.status_code == 200:
    timeline = json.loads(req.text)
    for tweet in timeline:
        print(tweet['user']['name']+'::'+tweet['text'])
        print(tweet['created_at'])
        print('----------------------------------------------------')
else:
    print("ERROR: %d" % req.status_code)

4.Code2##

次にキーワードを入れて検索し、任意の数表示。

search_twi.py
# -*- coding:utf-8 -*-
import json, config
from requests_oauthlib import OAuth1Session

# API認証情報
CK = config.CONSUMER_KEY
CS = config.CONSUMER_SECRET
AT = config.ACCESS_TOKEN
ATS = config.ACCESS_TOKEN_SECRET
twitter = OAuth1Session(CK,CS,AT,ATS)

# API取得
url = "https://api.twitter.com/1.1/search/tweets.json"

# 検索情報と検索件数の入力
print("何を調べますか?")
keyword = input(">> ")
print("何件表示しますか?")
num = input()
print("---------------------------------------")

# 検索情報と検索件数
params = {'q' : keyword, 'count' : num}

req = twitter.get(url, params = params)

if req.status_code == 200:
    #正常に接続できたら処理をする
    search_timeline = json.loads(req.text)
    for tweet in search_timeline['statuses']:
      print(tweet['user']['name'] + '::' + tweet['text'])
      print(tweet['created_at'])
      print('------------------------------------')
else:
    print("ERROR: %d" % req.status_code)  

5.Code3##

最後にツイートしてみる。

post_twi.py
# -*- coding:utf-8 -*-
import json, config
from requests_oauthlib import OAuth1Session

CK = config.CONSUMER_KEY
CS = config.CONSUMER_SECRET
AT = config.ACCESS_TOKEN
ATS = config.ACCESS_TOKEN_SECRET
twitter = OAuth1Session(CK,CS,AT,ATS)

url = "https://api.twitter.com/1.1/statuses/update.json"

# 投稿内容入力
print("呟く内容は?")
tweet = input('>> ')
print('-------------------------------')

# パラメータのセット
params = {"status" : tweet}

req = twitter.post(url, params = params)

if req.status_code == 200:
    #正常に処理できたら
    print("Success!")
else:
    print("ERROR : %d" % req.status_code)

6.メモ##

API初めて使ったけど、便利だし、Pythonだとスッキリ書ける。

8
16
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?