-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape_tweets.py
More file actions
24 lines (20 loc) · 785 Bytes
/
scrape_tweets.py
File metadata and controls
24 lines (20 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import csv
import tweepy
from tokens import *
def scrape_tweets(username: str, filename: str):
# Authenticate to Twitter
auth = tweepy.OAuthHandler(
consumer_key=CONSUMER_KEY, consumer_secret=CONSUMER_SECRET,
access_token=ACCESS_TOKEN, access_token_secret=ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
try:
api.verify_credentials()
except Exception as e:
exit(e)
tweets = api.user_timeline(screen_name=username, count=1000, include_rts=False, tweet_mode='extended')
tweets = [{"tweet": tweet.full_text} for tweet in tweets]
with open(filename, 'w', newline='') as f:
fieldnames = ["tweet"]
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(tweets)