Help us understand the problem. What is going on with this article?

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

背景

機械学習したいなーって思って自分のツイート履歴を取得してみた
ハッシュタグやメンションは除外するようにしたかったので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)

参考

https://cre8cre8.com/python/twitter-api.htm

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away