【发布时间】:2019-02-28 14:53:06
【问题描述】:
好的,这就是我所拥有的:
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)
>>>
===================
有人愿意帮助我吗?我一直在尝试解决这个问题,但我没有得到任何结果。
【问题讨论】:
-
关于 python 2.7 的 IDK(无论如何你都应该使用 3),但在 3 中你可以调用
decode('utf-8')(像这样:tweet.user.screen_name.decode('utf-8'))来解码它。任何人都很难找到答案,因为这个问题很难重现(: -
在使用 Unicode 时需要小心。不要将 Unicode 字符串与 UTF-8 字节混合。 Python 3 中的 Unicode 要容易得多。同时,请看nedbatchelder.com/text/unipain.html
标签: python api twitter bots tweepy