Skip to content

Instantly share code, notes, and snippets.

Created August 19, 2014 04:57
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Simple script to export Twitter lists to CSV
import twitter # python-twitter
import csv
import cStringIO
import codecs
from requests_oauthlib import OAuth1Session
class UnicodeWriter:
A CSV writer which will write rows to CSV file "f",
which is encoded in the given encoding.
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
# Redirect output to a queue
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds) = f
self.encoder = codecs.getincrementalencoder(encoding)()
def _encode_utf8(self, val):
if isinstance(val, (unicode, str)):
return val.encode('utf-8')
return val
def writerow(self, row):
self.writer.writerow([self._encode_utf8(s) for s in row])
# Fetch UTF-8 output from the queue ...
data = self.queue.getvalue()
data = data.decode("utf-8")
# ... and reencode it into the target encoding
data = self.encoder.encode(data)
# write to the target stream
# empty queue
def writerows(self, rows):
for row in rows:
def get_access_token(consumer_key, consumer_secret):
oauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret)
print 'Requesting temp token from Twitter'
resp = oauth_client.fetch_request_token(REQUEST_TOKEN_URL)
except ValueError, e:
print 'Invalid respond from Twitter requesting temp token: %s' % e
url = oauth_client.authorization_url(AUTHORIZATION_URL)
print ''
print 'I will try to start a browser to visit the following Twitter page'
print 'if a browser will not start, copy the URL to your browser'
print 'and retrieve the pincode to be used'
print 'in the next step to obtaining an Authentication Token:'
print ''
print url
print ''
pincode = raw_input('Pincode? ')
print ''
print 'Generating and signing request for an access token'
print ''
oauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret,
resp = oauth_client.fetch_access_token(ACCESS_TOKEN_URL)
except ValueError, e:
print 'Invalid respond from Twitter requesting access token: %s' % e
print 'Your Twitter Access Token key: %s' % resp.get('oauth_token')
print ' Access Token secret: %s' % resp.get('oauth_token_secret')
print ''
return resp.get('screen_name'), resp.get('oauth_token'), resp.get('oauth_token_secret')
if __name__ == "__main__":
consumer_key = raw_input('Enter your consumer key: ')
consumer_secret = raw_input("Enter your consumer secret: ")
screen_name, oauth_token, oauth_token_secret = get_access_token(consumer_key, consumer_secret)
api = twitter.Api(consumer_key=consumer_key, consumer_secret=consumer_secret,
access_token_key=oauth_token, access_token_secret=oauth_token_secret)
for l in api.GetListsList(screen_name):
print, l.slug
filename = "%s.csv" % l.full_name.replace('@', '').replace('/', '_')
f = open(filename, 'w')
writer = UnicodeWriter(f, quotechar='"')
writer.writerow(('name', 'url', 'description', 'followers_count', 'friends_count', 'listed_count', 'statuses_count', 'homepage'))
for m in api.GetListMembers(, l.slug):
writer.writerow((, '' + m.screen_name, m.description, m.followers_count, m.friends_count, m.listed_count, m.statuses_count, m.url))
Copy link

How i can use this?

Copy link

markgif commented Sep 2, 2019

I assume I need to get my own Twitter API token for this, is that correct? Thanks

Copy link

arikfr commented Sep 3, 2019


Note that this was written 5 years ago -- I'm not sure it still works.

Copy link

markgif commented Sep 5, 2019

5 years! Well thanks for writing it. After I get my twitter api key I plan to write my own.

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