我有以下代码,我想用这些代码收集定位到英国的推文,这些推文用英语编写,并带有与主题“死亡”和“共病”有关的关键字。我对这一切仍然很陌生,所以请忍受,代码绝对不是理想的。经过数小时的流传输后,我总是收到消息“ UnicodeEncodeError:'charmap'编解码器无法将字符'\ U0001f449'字符映射编码为追溯到stream.filter行(最后一行)。首先,我认为这是因为所有字符串所致,所以我在每个字符串前都添加了“ u”,但这没有帮助。
class StdOutListener(StreamListener):
def on_status(self, status):
if (u'death' in status.text.lower() or u'dead' in status.text.lower() or u'decease' in status.text.lower()) and (u'corona' in status.text.lower() or u'covid' in status.text.lower()):
print(status)
return True
def on_error(self, status_code):
print(error)
if __name__ == '__main__':
mystreamlistener = StdOutListener()
#This handles Twitter authentification and the connection to Twitter Streaming API
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, mystreamlistener)
# stream filtered by location in United Kingdom
stream.filter(locations=[-6.38,49.87,1.77,55.81], languages=[u'en'])