Get Tweets with Tweepy

Preface

--Getting a specific user's timeline to analyze Twitter in various ways --Acquired items (tweet ID, created_at, tweets, number of Favs, number of RTs) ――It is a humanities brain studying Python. Please note

environment

Preparation

--Install Tweepy -Register Twitter API

code

!/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)

# Get Tweets
tweet_data = []

 for tweet in tweepy.Cursor (api.user_timeline, screen_name = "Enter the name you want to get here", exclude_replies = True) .items ():
    tweet_data.append([tweet.id,tweet.created_at,tweet.text.replace('\n',''),tweet.favorite_count,tweet.retweet_count])

# csv output
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

Impressions

――It was very clean. Long live Tweepy! ――I still don't understand the details. ――I want to do natural language processing using this data.

Reference URL

-How to use Twitter REST API

Recommended Posts

Get Tweets with Tweepy
Get replies to specific tweets with tweepy
Get lots of your tweets with Tweepy
Get tweets containing keywords using Python Tweepy
Exclude tweets containing URLs with tweepy [Python]
Posting tweets with python
Get date with python
Get started with Mezzanine
Get media timeline images and videos with Python + Tweepy
Get twitter tweets, followers, followers, likes, with urllib and beautifulSoup
Get tweets with arbitrary keywords using Twitter's Streaming API
Get country code with python
Get table dynamically with sqlalchemy
Get started with Django! ~ Tutorial ⑤ ~
Get Twitter timeline with python
Get Youtube data with python
Get information with zabbix api
Get started with Django! ~ Tutorial ④ ~
Get started with Django! ~ Tutorial ⑥ ~
Get thread ID with python
Get started with Python! ~ ② Grammar ~
Get image features with OpenCV
Search twitter tweets with python
Get stock price with Python
Get home directory with python
Get keyboard events with python
Get Alembic information with Python
Get another tab with pyppeteer
Get ranking with Rakuten API
How to cancel RT with tweepy
Get started with Python! ~ ① Environment construction ~
Link to get started with python
Get reviews with python googlemap api
Get started with MicroPython (on macOS)
Get only image tweets on twitter
Get the weather with Python requests
Get web screen capture with python
Get the weather with Python requests 2
Get one column from DataFrame with DataFrame
[Python] Get economic data with DataReader
How to get started with Scrapy
[Small story] Get timestamp with Python
How to get started with Django
Get Qiita trends with Python scraping
Get Redmine User list with DataFrame
Get motor angles with Python-LEGO Mindstorms
Get data from Twitter using Tweepy
Get started with machine learning with SageMaker
Get started with Python in Blender
Get weather information with Python & scraping
A story I was addicted to trying to get a video url with tweepy
Get additional data in LDAP with python
Get property information by scraping with python
Step notes to get started with django
Touch around the twitter list with tweepy
Get html from element with Python selenium
[Note] Get data from PostgreSQL with Python
How to get parent id with sqlalchemy
I tried to get started with Hy
CRUD GET with Nuxt & Django REST Framework ②
[Python] Get the variable name with str