Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What is going on with this article?
@nya3_neko2

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

More than 1 year has passed since last update.

背景

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

参考

2
Help us understand the problem. What is going on with this article?
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
nya3_neko2
ますます数学

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
2
Help us understand the problem. What is going on with this article?