Вопрос

I am working on a project for which I need to extract tweets. I have used tweepy in python for the same, using various sources of information available online as I'm not very comfortable with python. I am coming across two issues I haven't been able to resolve by googling! 1) I want approx 1000 to get stored in a file. I guess I could use a count variable for the same but don't know where and how to use it. Basically, how to terminate the program once I get 1000 tweets? 2) When printing to file, I get an error that reads, " File "Tweet3.py", line 20, in on_status print "Tweet Text : %s"%status.text UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 65: ordinal not in range(128)" - How could I possibly resolve this error?

Here is the code:

import sys
import tweepy
import webbrowser
fp=open("Tweets.txt","w")

Q=['Earthquake','Flood']#Filters

c_key = '...'
c_secret = '...'
a_token= '...'
a_token_sec= '...'

auth = tweepy.OAuthHandler(c_key, c_secret)
auth.set_access_token(a_token, a_token_sec)

class CustomStreamListener(tweepy.StreamListener):

     def on_status(self, status):
        print "----------NEW TWEET!-----------"
        print "Tweet Text : %s"%status.text
        fp.write(status.text) 
        print "Author's name : %s"%status.author.screen_name 
        print "Time/Date of creation : %s"%status.created_at
        print "Source of Tweet : %s"%status.source
        print "Coordinates : %s"%status.coordinates

streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60)
print "Displaying Tweets for filters :"
#print Q
#streaming_api.filter(follow=None, track=Q)
streaming_api.filter(locations=[-125,25,-65,48], async=False)
Это было полезно?

Решение

Encode the text first before writing it to the file:

status.text.encode('utf8')

EDIT:

Try this instead:

import codecs
fp = codecs.open("Tweets.txt", "w", "utf-8")
fp.write(status.text) 

EDIT:

Create a counter and increment it every time a new tweet occurs, e.g.:

counter = 0
MAX_TWEETS = 1000

within the on_status method:

counter += 1
if counter >= MAX_TWEETS:
    sys.exit()
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top