Skip to main content
 首页 » 编程设计

python之Tweepy 数据未以 JSON 格式显示

2025年05月04日16think

我使用 Tweepy 已经有一段时间了。我使用了他们的 api.search 和流,一切正常,我可以很好地提取数据。

当我开始使用 api.lookup_users 时,我的结果没有以 json 格式显示,而是以其他一些我无法提取数据的格式显示:

tweet.text 

或

tweet.username 

这是我正在使用的用户查找代码:

user_id = api.lookup_users(user_ids=[2198765],include_entities=['True']) 
 
print(user_id)  

这是我从 lookup_users 获得的部分数据:

[ 
User(profile_background_tile=False, 
profile_location=None, 
url='http: //t.co/7kRL5urm0d', 
favourites_count=1262, 
profile_text_color='000000', 
notifications=False, 
profile_background_image_url='http: //abs.twimg.com/images/themes/theme1/bg.png', 
followers_count=10356, 
lang='en', 
is_translation_enabled=False, 
is_translator=False, 
contributors_enabled=False, 
default_profile=False, 
entities={ 
    'url': { 
        'urls': [ 
            { 

我确实通过 JSONValidator 运行了验证,正如预期的那样,出现了一大堆错误。

Parse error on line 1: 
[    User(profile_backgro 
-----^ 
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', ']' 

** 编辑:

所以我又四处看了看,发现 api.search 会在 <class 'tweepy.models.Status'> 中给出结果。而 api.lookup_users 在 <class 'tweepy.models.ResultSet'> 中.关于如何改变它的任何想法?

请您参考如下方法:

您正在获取 Model 实例列表。通过查看代码,可以通过修改 tweepy/binder.py 模块中的 self.parser = kwargs.pop('parser', api.parser) 轻松覆盖此行为.您可以使用配置文件提供不同的解析器。