【发布时间】:2016-09-12 15:35:24
【问题描述】:
我的问题
当使用 tweepy 流式传输数据时,我收到了
的预期结果Tweet Contents: RT @ChickSoPretty: Zendaya tho \ud83d\ude4c https:....
使用代码时
def on_data(self, data):
username = data.split(',"screen_name":"')[1].split('","location"')[0]
tweet = data.split(',"text":"')[1].split('","source')[0]
print("Tweet Contents: " + tweet)
--- 我目前正在跟踪 u'\U0001f64c',一个表情符号的代码。 ---
但是,当我尝试输出其余用户最近的推文时...
for status in tweepy.Cursor(api.user_timeline, id=username).items(20):
tweet = status.text
print("Tweet Contents: " + tweet)
如果“用户名”是最近使用过表情符号的用户,我的程序会崩溃。
这是可以理解的,因为我现在正尝试在控制台上打印一个表情符号,而不是我最初所做的,而是显示 Javascript 转义代码 \ud83d\ude4c。。 p>
我的问题是,我如何阅读用户的状态并以第一种格式输出他们的推文?
我的代码的目的
我的长期目标是遍历用户的状态,并检查他们在最近的 20 条推文中使用了多少表情符号(包括 RT 和回复)。
当表情符号以 Javascript/Java Escape 格式显示时,我已经“成功创建”了一些用于检测推文中表情符号的杂乱代码,如下...
for character in tweet:
iteration = iteration + 1
if(iteration < tweetLength):
if tweet[iteration] == '\\' and tweet[iteration + 1] == 'u' and tweet[iteration + 6] == '\\' and tweet[iteration + 7] == 'u':
for x in range(0,12):
emojiCode += tweet[iteration + x]
numberOfEmojis = numberOfEmojis + 1
print("Emoji Code Found: "+emojiCode)
emojiCode = ""
iteration = iteration + 7
哇,真是一团糟。但是,它适用于我需要它做的事情(仅限英文推文)。
有没有更好的方法?我应该废弃这个并使用
tweet.encode('utf-8')
并尝试查找以下输出格式的表情符号?
b'@Jathey3 @zachnahra31 this hard\xf0\x9f\x98\x82 we gotta do this https:...'
我正在使用 Python 3.4.2
【问题讨论】:
标签: python twitter unicode tweepy emoji