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

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
35
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

@i_am_miko

Tweepyでツイートを取得

前書き

  • Twitterを色々分析するために、特定のユーザーのタイムラインを取得した
  • 取得したもの(ツイートID・created_at・ツイート・Fav数・RT数)
  • Python勉強中の文系脳です。ご了承ください

環境

  • MacOS Sierra(10.12.6)
  • Python 3.6.1
  • tweepy 3.5.0

準備

コード

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import tweepy
import csv

consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)

#ツイート取得
tweet_data = []

for tweet in tweepy.Cursor(api.user_timeline,screen_name = "取得したい垢名をここに入れる",exclude_replies = True).items():
    tweet_data.append([tweet.id,tweet.created_at,tweet.text.replace('\n',''),tweet.favorite_count,tweet.retweet_count])

#csv出力
with open('tweets_20170805.csv', 'w',newline='',encoding='utf-8') as f:
    writer = csv.writer(f, lineterminator='\n')
    writer.writerow(["id","created_at","text","fav","RT"])
    writer.writerows(tweet_data)
pass

感想

  • とてもきれいに取れました。Tweepy万歳!
  • まだまだ細かい部分がよくわかってません。
  • このデータを使って自然言語処理をやってみたい。

参考URL

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
35
Help us understand the problem. What are the problem?