【问题标题】:UnicodeEncodeError with Tweepy when trying to save Stream of Tweets as json尝试将推文流保存为 json 时出现 Tweepy 的 UnicodeEncodeError
【发布时间】:2017-07-31 17:08:28
【问题描述】:

所以我正在尝试保存我在 Python 上通过 Tweepy 流式传输的推文。我正在使用此代码-->

class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
    print (status.text)

def on_data(self, data):
    json_data = json.loads(data)
    file.write(str(json_data))

def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True


sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
sapi.filter(track=['#GDPR'])

所以当我运行代码时,控制台会抛出这个 -->

sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
sapi.filter(track=['GDPR'])
Traceback (most recent call last):

File "<ipython-input-20-564026ea611c>", line 2, in <module>
sapi.filter(track=['GDPR'])

File "/Users/Sweeties/anaconda/lib/python3.6/site-packages/tweepy/streaming.py", line 445, in filter
self._start(async)

File "/Users/Sweeties/anaconda/lib/python3.6/site-packages/tweepy/streaming.py", line 361, in _start
self._run()

File "/Users/Sweeties/anaconda/lib/python3.6/site-packages/tweepy/streaming.py", line 294, in _run
raise exception

File "/Users/Sweeties/anaconda/lib/python3.6/site-packages/tweepy/streaming.py", line 263, in _run
self._read_loop(resp)

File "/Users/Sweeties/anaconda/lib/python3.6/site-packages/tweepy/streaming.py", line 324, in _read_loop
self._data(next_status_obj)

File "/Users/Sweeties/anaconda/lib/python3.6/site-packages/tweepy/streaming.py", line 297, in _data
if self.listener.on_data(data) is False:

File "<ipython-input-16-674fb26e0cb4>", line 7, in on_data
file.write(str(json_data))

UnicodeEncodeError: 'ascii' codec can't encode characters in position 133-134: ordinal not in range(128)

我尝试在谷歌上搜索答案,但找不到与此特定问题相关的任何内容。有人可以帮我吗?

【问题讨论】:

    标签: python json python-3.x encoding tweepy


    【解决方案1】:

    您有 Python 2 str 不支持的非 ascii 字符。只需去掉你 file.write(...) 中的 str()

    【讨论】:

    • 我把它改成了这个: def on_data(self, data): json_data = json.loads(data) file.write(json_data) 结果我得到了这个 --> TypeError: write () 参数必须是 str,而不是 dict
    • 你需要序列化字典。好像是json。然后使用 file.write(json.dumps(data, ensure_ascii=False))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2022-01-15
    • 2014-06-25
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    相关资源
    最近更新 更多