LoginSignup
33
28

More than 5 years have passed since last update.

Pythonでツイート収集

Last updated at Posted at 2016-01-30

pythonでツイートを収集します。
主に、機械学習する際に〇〇botなどから情報を得たい時に使用します。(私の場合は)
〇〇botなどは非常に内容が偏っており、
例えばツンデレbotなどではツンデレに関するツイートが多いと言えます。
これはこのアカウントをツンデレのセリフ集であり、全てがツンデレセリフであると認識でき、わかりやすく言えば、「スパムであるかそうでないか」つまりツンデレかツンデレでないかという分類のツンデレであるのモデルに使用できます。

前置きが長くなりましたが、ツイート取得するコードがほんの数行です。
LibraryはTweepyを使用していますのでインストールしていない場合は

コマンド
pip install tweepy
or
sudo easy_install tweepy

でインストールできます。

実際に動かすにはCK, CS, AK, ATを取得する必要があります。
下記URLから新しいAppを作成してTokenを取得してください
https://apps.twitter.com/
get_timeline()にはそれぞれ
取得したいアカウントの(id),
取得するツイートの数(cout)
を当てはめます。

get_timeline
#coding: utf-8
import tweepy

CK = ""
CS = ""
AK = ""
AT = ""

def get_twitter_api(CK, CS, AK, AT):
    auth = tweepy.OAuthHandler(CK, CS)
    auth.set_access_token(AK, AT)
    api = tweepy.API(auth)
    return api

def get_timeline(id, count=100):
    API = get_twitter_api(CK, CS, AK, AT)
    i = API.user_timeline(id=id, count=count)
    with open("", "w") as f:
        for k in i:
            encode = k.text+'\n'
            f.write(encode.encode('utf-8'))

if __name__ == '__main__':
    get_timeline(id="", count=100)
33
28
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
33
28