LoginSignup
1
2

More than 3 years have passed since last update.

自分のツイートの文章のみを取得する

Last updated at Posted at 2019-05-05

背景

機械学習したいなーって思って自分のツイート履歴を取得してみた
ハッシュタグやメンションは除外するようにしたかったのでregexを使いました
本当はツイッターの公式が自分のツイート履歴はデータをくれるのでわざわざAPI叩く必要はないです
https://qiita.com/nya3_neko2/items/50d8ba2228f22fac9d3d

除外対象 

ツイート自体
・RT
部分的に除外
・メンション idを除外
・ハッシュタグ ハッシュタグのみを除外

コード

import json, re, time
from requests_oauthlib import OAuth1Session

CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_TOKEN_SECRET = ''

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"

params = {'count': 200}
req = twitter.get(url, params=params)

txt_file = open('learn_text.txt','a')

if req.status_code == 200:
    timeline = json.loads(req.text)
    for tweet in timeline:
        tw_txt = tweet['text']
        regex_at = re.compile('@\S+ ?')
        regex_http = re.compile('https://\S+')
        regex_tag = re.compile('#\S+ ?')
        regex_end = re.compile('\n$')
        if len(re.findall('^RT', tw_txt)) == 0:
            tw_txt=regex_at.sub('',tw_txt)
            tw_txt = regex_http.sub('', tw_txt)
            tw_txt = regex_tag.sub('', tw_txt)
            if regex_end.search(tw_txt) == None:
                tw_txt = tw_txt + '\n'
            txt_file.write(tw_txt)

else:
    print("ERROR: %d" % req.status_code)

txt_file.close()

txt_file = open('learn_text.txt')
content=txt_file.read()
txt_file.close()
print(content)

参考

1
2
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
1
2