Skip to content

Instantly share code, notes, and snippets.

@benjavides
Created May 29, 2016 16:22
Show Gist options
  • Select an option

  • Save benjavides/cc37a5708752e9764caa269b15019aff to your computer and use it in GitHub Desktop.

Select an option

Save benjavides/cc37a5708752e9764caa269b15019aff to your computer and use it in GitHub Desktop.
Descarga todos los tweets con las palabras indicadas en un circulo con el radio especificado y los guarda en un .csv
import tweepy
import csv
import unicodedata
# Go to http://apps.twitter.com and create an app.
# The consumer key and secret will be generated for you after
consumer_key=''
consumer_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=''
access_token_secret=''
def replace(s):
return s.replace('\xc3\xa1','á').replace('\xc3\xa9','é').replace('','í').replace('\xc3\xb3','ó').replace('','ú').replace('\xc3\xb1','ñ').replace('\uf8ff','@')
def elimina_tildes(s):
return ''.join((c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn'))
if __name__ == '__main__':
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
file_name = "positivo_negativo"
csvFile = open('{0}.csv'.format(file_name), 'w', encoding="utf-8")
csvWriter = csv.writer(csvFile, delimiter='|',quotechar=' ', quoting=csv.QUOTE_MINIMAL)
j=0 #tweets_total
coord = [-33.446243, -70.660528] #baquedano
for tweet in tweepy.Cursor(api.search,q=":‑) OR :) OR :D OR :o) OR :] OR :3 OR :c) OR :> OR =] OR 8) OR =) OR :} OR :^) OR :っ) OR >:[ OR :‑( OR :( OR :‑c OR :c OR :‑< OR :っ OR C:< OR :‑[ OR :[ OR :{",since="2016-05-15",until="2016-05-26",wait_on_rate_limit=True,wait_on_rate_limit_notify=True,geocode="{0},{1},10km".format(coord[0],coord[1])).items():
if tweet.entities.get("hashtags"):
hashtags = str([d['text'] for d in tweet.entities.get("hashtags")]).strip("[]")
else:
hashtags = ""
texto = elimina_tildes(tweet.text.replace('|', ' ').replace('\n', ' '))
username = elimina_tildes(tweet.user.screen_name)
if tweet.coordinates:
coordinate = tweet.coordinates["coordinates"]
csvWriter.writerow(username,tweet.created_at, texto,hashtags,coordinate[0],coordinate[1]])
else:
csvWriter.writerow([username,tweet.created_at,texto,hashtags])
j+=1
print("vamos en el tweet n°:",j)
print("Se decargaron {0} tweets".format(j))
csvFile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment