0

我正在尝试使用 twitter 模块和 python 从 twitter 开始作弊数据。这是我的代码

import twitter

import win_unicode_console
win_unicode_console.enable()


CONSUMER_KEY = 'xxxxxxxxxxxxxxxxxx'
CONSUMER_SECRET = 'xxxxxxxxxxxxxxx'
OAUTH_TOKEN = 'xxxxxxxxxxxxxxxxx'
OAUTH_TOKEN_SECRET = 'xxxxxxxxxxxx'

auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
                       CONSUMER_KEY, CONSUMER_SECRET)
twitter_api = twitter.Twitter(auth=auth)

print(twitter_api)

WORLD_WOE_ID = 1
US_WOE_ID = 23424977

world_trends = twitter_api.trends.place(_id=WORLD_WOE_ID)
us_trends = twitter_api.trends.place(_id=US_WOE_ID)

print(us_trends)
print(world_trends)

我收到编码错误。所以我用

print((us_trends).encode('utf-8'))

这导致

AttributeError: 'TwitterListResponse' object has no attribute 'encode'

所以我决定使用 win_unicode_console 模块

但令人困惑的是 us_trends 正在返回值。

[{'trends': [{'name': 'El Chapo', 'url': 'http://twitter.com/search?q=%22El+Chapo%22', 'promoted_content': None, 'query': '%22El+Chapo%22', 'tweet_volume': 103536}, {'name': 'Antonio Brown', 'url': 'http://twitter.com/search?q=%22Antonio+Brown%22', 'promoted_

但声明

print(world_trends)

给出以下错误

File "C:\Users\nawendu\Desktop\TWIT.PY", line 25, in <module>
print(world_trends)
File 
line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 24- 
29: character maps to <undefined>

编码如何在我们的趋势中而不是在世界趋势中起作用?

4

1 回答 1

0

encode是一个字符串的方法。

你有一个 json 对象,它没有这个方法。

当你print是一个对象时,它需要将对象转换为你的输出编码的字符串表示形式(这里可能是 windows 编码)。如果其中有字符(例如表情符号)不在输出编码中,则会出现错误。

编码是一个困难的话题(也是 Python 中的一个痛点),但如果你想打印输出,你需要了解它们。

于 2019-02-13T07:03:07.910 回答