【问题标题】:how to get only the text of the tweets into a json file如何仅将推文的文本获取到 json 文件中
【发布时间】:2020-09-05 03:19:30
【问题描述】:

所以我正在开发一个项目,使用 twitterAPI 来收集使用特定经度和纬度的不同关键字的推文。我为我的推文抓取了数据,数据是每个关键字的字典列表,其中包含以下字段:

dict_keys(['created_at', 'id', 'id_str', 'text', 'truncated', 'entities', 'extended_entities', 'metadata', 'source', 'in_reply_to_status_id', 'in_reply_to_status_id_str', 'in_reply_to_user_id', 'in_reply_to_user_id_str', 'in_reply_to_screen_name', 'user', 'geo', 'coordinates', 'place', 'contributors', 'is_quote_status', 'retweet_count', 'favorite_count', 'favorited', 'retweeted', 'possibly_sensitive', 'lang'])

我现在想将每个关键字 x1_tweets、x2_tweets 和 x3_tweets 的文本提取到一个 json 文件中

为此我定义了一个函数:

def save_to_json(obj, filename):
    with open(filename, 'w') as fp:
        json.dump(obj, fp, indent=4, sort_keys=True) 

其中 obj 是字典列表,filename 是我要用来保存文档的文件名。当我尝试使用该功能时,例如save_to_json(x1_tweets, doors) 它会返回一个包含其中所有内容的文件。我应该如何使用它返回一个只包含推文的文件的函数? 任何帮助将不胜感激!提前致谢! 这是 json 文件的样子:

[
    {
        "contributors": null,
        "coordinates": null,
        "created_at": "Mon May 18 02:08:53 +0000 2020",
        "entities": {
            "hashtags": [],
            "media": [
                {
                    "display_url": "pic.twitter.com/ig7H0jIHOq",
                    "expanded_url": "https://twitter.com/CMag051/status/1262303473682022400/photo/1",
                    "id": 1262203448080007168,
                    "id_str": "1262203448080007168",
                    "indices": [
                        98,
                        121
                    ],
                    "media_url": "http://pbs.twimg.com/media/EYQ_WT0VAAA6hTK.jpg",
                    "media_url_https": "https://pbs.twimg.com/media/EYQ_WT0VAAA6hTK.jpg",
                    "sizes": {
                        "large": {
                            "h": 2048,
                            "resize": "fit",
                            "w": 1536
                        },
                        "medium": {
                            "h": 1200,
                            "resize": "fit",
                            "w": 900
                        },
                        "small": {
                            "h": 680,
                            "resize": "fit",
                            "w": 510
                        },
                        "thumb": {
                            "h": 150,
                            "resize": "crop",
                            "w": 150
                        }
                    },
                    "type": "photo",
                    "url": "https://twitter.com/ig7H0jIHOq"
                }
            ],
            "symbols": [],
            "urls": [],
            "user_mentions": []
        },
        "extended_entities": {
            "media": [
                {
                    "display_url": "pic.twitter.com/ig7H0jvHOq",
                    "expanded_url": "https://twitter.com/CMag051/status/1262253473682022400/photo/1",
                    "id": 1262203448080007168,
                    "id_str": "1262203448080007168",
                    "indices": [
                        98,
                        121
                    ],
                    "media_url": "http://pbs.twimg.com/media/EYQ_WT0VAAA6hTK.jpg",
                    "media_url_https": "https://pbs.twimg.com/media/EYQ_WT0VAAA6hTK.jpg",
                    "sizes": {
                        "large": {
                            "h": 2048,
                            "resize": "fit",
                            "w": 1536
                        },
                        "medium": {
                            "h": 1200,
                            "resize": "fit",
                            "w": 900
                        },
                        "small": {
                            "h": 680,
                            "resize": "fit",
                            "w": 510
                        },
                        "thumb": {
                            "h": 150,
                            "resize": "crop",
                            "w": 150
                        }
                    },
                    "type": "photo",
                    "url": "https://twitter.com/ig7H0iIHOq"
                }
            ]
        },
        "favorite_count": 1,
        "favorited": false,
        "geo": null,
        "id": 1262203473682022400,
        "id_str": "1262203473682022400",
        "in_reply_to_screen_name": null,
        "in_reply_to_status_id": null,
        "in_reply_to_status_id_str": null,
        "in_reply_to_user_id": null,
        "in_reply_to_user_id_str": null,
        "is_quote_status": false,
        "lang": "en",
        "metadata": {
            "iso_language_code": "en",
            "result_type": "recent"
        },
        "place": null,
        "possibly_sensitive": false,
        "retweet_count": 0,
        "retweeted": false,
        "source": "<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>",
        "text": "Beautiful evening. \n\nSitting on patio, eating some apple , and listening to the birds chirp. https://twitter.com/ig7H0jIHOq",
        "truncated": false,
        "user": {
            "contributors_enabled": false,
            "created_at": "Wed Apr 01 03:32:05 +0000 2009",
            "default_profile": false,
            "default_profile_image": false,
            "description": "Photographer | Music & Sports Enthusiast.",
            "entities": {
                "description": {
                    "urls": []
                }
            },
            "favourites_count": 19189,
            "follow_request_sent": false,
            "followers_count": 547,
            "following": false,
            "friends_count": 2432,
            "geo_enabled": false,
            "has_extended_profile": true,
            "id": 28041855,
            "id_str": "28041855",
            "is_translation_enabled": false,
            "is_translator": false,
            "lang": null,
            "listed_count": 0,
            "location": "Phoenix, AZ",
            "name": "Chris",
            "notifications": false,
            "profile_background_color": "000000",
            "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png",
            "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png",
            "profile_background_tile": false,
            "profile_banner_url": "https://pbs.twimg.com/profile_banners/28041855/1586840506",
            "profile_image_url": "http://pbs.twimg.com/profile_images/1262196071817605121/WBvC3h5P_normal.jpg",
            "profile_image_url_https": "https://pbs.twimg.com/profile_images/1262196071817605121/WBvC3h5P_normal.jpg",
            "profile_link_color": "ABB8C2",
            "profile_sidebar_border_color": "000000",
            "profile_sidebar_fill_color": "000000",
            "profile_text_color": "000000",
            "profile_use_background_image": false,
            "protected": false,
            "screen_name": "CMag051",
            "statuses_count": 11285,
            "time_zone": null,
            "translator_type": "none",
            "url": null,
            "utc_offset": null,
            "verified": false
        }
    }

【问题讨论】:

  • 你有json文件的样例吗?
  • 嗨@alvas,我在上面的帖子中包含了一部分 json 文件,谢谢!
  • 谁能帮忙?
  • 加载时您的 json 定义不明确,在您首先修复之前帮不上什么忙 =)
  • 你能详细说明你的意思吗?

标签: python json python-3.x twitter twitterapi-python


【解决方案1】:

您需要做的第一件事是将以下代码更改为:

def save_to_json(obj, filename):
    with open(filename, 'a') as fp:
        json.dump(obj, fp, indent=4, sort_keys=True) 

由于以下原因,您需要更改打开文件的模式。

w: 以只写模式打开。指针位于文件的开头,这将覆盖任何现有的同名文件。如果不存在同名文件,它将创建一个新文件。

a: 打开一个文件以添加新信息。指针位于文件末尾。如果不存在同名文件,则会创建一个新文件。

此外,sort_keys 没有任何意义,因为您只传递了 string 而不是 dict。同样,indent=4strings 没有意义。

如果您需要对推文文本进行索引,您可以使用以下代码:

tweets = {}    
for i, tweet in enumerate(x1_tweets):
    tweets[i] = tweet['text']
save_to_json(tweets,'bat.json')

上面的代码将创建一个dict,其中包含推文的索引,并在处理完所有推文后写入文件。

如果您只需要没有索引的推文文本,您可以使用string aggregation 或使用listappend 推文中的所有文本list 并将其写入输出文件。

【讨论】:

  • 如果您也可以提及输出文件的格式会很好,因为它可以帮助我适当地更新该代码。
  • 嗨,我希望输出文件的格式仅包含推文而不包含索引,我应该如何修改代码?不包括 tweets[i] 返回一个空字典
  • 你能告诉我为什么最后一条推文没有显示在 json 文件中吗?例如,我有 100 条针对某个关键字的推文,但只显示了 99 条?
  • @karansethi,用于索引计数从 0 开始,一直到 99,因此您总共有 100 条推文。对于您想要的格式,您可以使用:And if you just need the text of the tweets without the index you can use string aggregation or use list and append all the text from a tweet in that list and write that to the output file. 以上答案中已经提到。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-20
  • 1970-01-01
  • 2015-10-10
  • 1970-01-01
  • 2018-04-07
  • 2020-10-05
  • 1970-01-01
相关资源
最近更新 更多