Tweepy API中的“ UnicodeDecodeError”

时间:2018-09-25 04:05:59

标签: python api twitter bots tweepy

好的,这就是我所拥有的:

    import tweepy 
    import time

    #login credentials twitter account
    consumer_key = '------'
    consumer_secret = '-----'
    access_token = '-----'
    access_secret = '-----'

    #login
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_secret)
    api = tweepy.API(auth)
    search_query = "I'm"


    user = api.me()
    print(user.name)

    max_tweets = 100

    #reply
    for tweet in tweepy.Cursor(api.search,         q=search_query).items(max_tweets):
        c=tweet.text.encode('utf8')
        c=c.replace("im ","")
        answer="@"+tweet.user.screen_name+" Hi " + c + ", I'm Dad!"
        print ("Reply:",answer)

   api.update_status(status=answer,in_reply_to_status_id=tweet.id)
   time.sleep(300) #every 5 minutes

现在,我收到的错误是:

 Traceback (most recent call last):
      File "C:\Python27\twitterbotdad.py", line 26, in <module>
        answer="@"+tweet.user.screen_name+" Hi " + c + ", I'm Dad!"
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 33: ordinal not in range(128)
    >>> 
    =================== 

有人愿意协助我吗?我一直在尝试解决此问题,但是我什么都没得到。

1 个答案:

答案 0 :(得分:0)

您是否尝试过将以下行放在代码之上?

import sys
reload(sys)
sys.setdefaultencoding('utf8')

几天前,我还编辑了my answer to your query,以防您看不到。 :)

/ ogs