LoginSignup
7
6

More than 5 years have passed since last update.

今更ながら、pythonでTwitterを操作してみた

Posted at

Pythonに慣れようと思い、今更ながら、Twitterを操作してみました。

今回は、Twitterの各種トークンを発行と、自分の発言を取得してみました。

Twitterの各種トークン

https://apps.twitter.com」にアクセスし、
[Create New App]で新規作成

スクリーンショット 2018-05-06 17.49.50.png

赤印のとこを記載。
注意としては、自分のアカウントに電話番号の登録をしておくことです。

作成が終わったら、上記タブの「Keys and Access Tokens」を選択して、各種トークンを発行します。
必要なトークンは、

Consumer Key (API Key) 

Consumer Secret (API Secret)

Access Token

Access Token Secret

ライブラリのインストール

 pip install python-twitter

上記コマンドでインストールします

コード

import twitter

api =twitter.Api(consumer_key='',
              consumer_secret='',
              access_token_key='',
              access_token_secret=''
              )

statuse = api.GetUserTimeline(api.VerifyCredentials().id, count=10)

for c in coment:
    print(c.text)

今回は、自分の発言を最新から10件取得してみます。
「count」の数字を変えれば、最大で200件取得できます。

エラー

AttributeError: module 'twitter' has no attribute 'Api'
上記のエラーが出ました。
どうやら、ファイルの名前を「twitter.py」にしていたために、競合してPCがわけわかんないと拗ねたよう。
ファイルの名前を「test_twitter.py」に変更して、再度実行。

結果

スクリーンショット 2018-05-06 18.11.25.png
無事できました。
改めて、Python凄いなって思いました。
今後もPythonで色々やっていきたいと思います。

参考資料
https://github.com/bear/python-twitter#api

7
6
0

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
7
6