如何解决此错误消息,该错误是什么意思?

时间:2020-06-11 17:13:37

标签: python json twitter tweepy

我正在使用Twitter API流推文,并且不断收到此类错误。我该如何解决它们?某些推文成功流式传输,但是大多数时候尝试流式传输时出现这些错误。

许多错误中的一些(我得到的所有错误都与这些错误类似)

    ('failed ondata', "'ascii' codec can't encode character u'\\u2026' in position 139: ordinal not in range(128)")
    ('failed ondata', "'ascii' codec can't encode characters in position 86-89: ordinal not in range(128)")
    ('failed ondata', "'ascii' codec can't encode character u'\\u2026' in position 139: ordinal not in range(128)")
    ('failed ondata', "'ascii' codec can't encode character u'\\u2026' in position 139: ordinal not in range(128)")

.py文件

from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import time
import json
import csv
import sys
import Tweet
from DatabaseInteractor import * 
import twitter_credentials




class TweetStream(StreamListener):


    def __unicode__(self):
        return self.some_field or u'None'

    def on_data(self, data):

        try:

            tweet_json = json.loads(data)
            text = str(tweet_json["text"])  # status.text
            username = str(tweet_json["user"]["screen_name"])
            location = str(tweet_json["user"]["location"])
            created_at = str(tweet_json["created_at"])
            tweet_id = str(tweet_json["id_str"])
            geo = str(tweet_json["geo"])
            coordinates = str(tweet_json["coordinates"])
            user_id = str(tweet_json["user"]["id_str"])
            hashtags = str(tweet_json["entities"]["hashtags"])
            reload(sys)
            sys.setdefaultencoding('utf-8')
           # print(str.encode(text))
            #print(str.encode(username))
            #print(str.encode(location))
            #print(str.encode(created_at))
            #print(str.encode(tweet_id))
            #print(str.encode(geo))
            #print(str.encode(coordinates))

           # print(str(user_id))
            #print(str.encode(hashtags))
            #tweet = Tweet.Tweet(user_id, username, tweet_id,text, hashtags, created_at, location, coordinates, geo)

           # tweet.manual_filter()
           # tweet.store_in_db()

            # open a csv which we will append to
           # saveTweets = open('SaveTweets.csv', 'a')
           # saveTweets.write(" { " + " ( " + u"created_at : " + created_at + " )" + " ( " + u" Username : " + username + " ) " + " ( " +
             #                u"Tweet Text : " + text + " ) " + " ( " + u" Tweet id " + tweet_id + " ) " + " ( " + u"Location : " + location + " ) " + " } ")
            #saveTweets.write('\n')
            #saveTweets.close()

        except BaseException, e:
            print('failed ondata', str(e))
            time.sleep(5)


def on_error(self, status):
    print(status)


def filter_tweet(text):
    pass


auth = OAuthHandler(twitter_credentials.API_KEY,
                    twitter_credentials.API_SECRET_KEY)
auth.set_access_token(twitter_credentials.ACCESS_TOKEN,
                      twitter_credentials.ACCESS_TOKEN_SECRET)

twitterStream = Stream(auth=auth, listener=TweetStream(),tweet_mode='extended')
twitterStream.filter(track=["plumber"])

0 个答案:

没有答案