Skip to content

Instantly share code, notes, and snippets.

@revox
Last active May 28, 2021 14:12
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save revox/3eb8136d61956a7ed8e7 to your computer and use it in GitHub Desktop.
Save revox/3eb8136d61956a7ed8e7 to your computer and use it in GitHub Desktop.
Use tweepy to write some tweet info into a CSV file
import time
import tweepy
import csv
# == OAuth Authentication ==
# The consumer keys can be found on your application's Details
# page located at https://dev.twitter.com/apps (under "OAuth settings")
consumer_key="your key"
consumer_secret="your secret"
# After the step above, you will be redirected to your app's page.
# Create an access token under the the "Your access token" section
access_token="your token"
access_token_secret="your token"
# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# Creation of the actual interface, using authentication
api = tweepy.API(auth)
results = api.search('#fracking', count=100)
csvfile = open('my_scraped_tweets.csv','wb')
csvwriter = csv.writer(csvfile)
for item in results:
# write out the user, the tweet and their follower count into a file
# the unicode bits are required to write non ASCII language bits into the file
csvwriter.writerow([unicode(item.user.screen_name).encode("utf-8"),unicode(item.text).encode("utf-8"),item.user.followers_count])
# time.sleep(1)
@kevinSJ27
Copy link

when I try this I get the following error:

csvwriter.writerow([unicode(item.user.screen_name).encode("utf-8"),unicode(item.text).encode("utf-8"),item.user.followers_count])
NameError: name 'unicode' is not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment