删除JSON/Python中twitters extended_tweet列的不必要详细信息

2024-10-03 11:21:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我用推特刮板下载了一些关于上次体育赛事的推特。不幸的是,由于研究的性质,我不能回去修改我的刮板,因为事件不会再次发生。推文分为几个类别,如时间戳、创建日期等

这些tweet存储在JSON文件中,我目前正在将它们导出到pandas

我关注的是每条推文细节中的文本和扩展的推文类别

不久前,Twitter允许用户发布更长的推文。当涉及到抓取推特数据时,如果推特在初始字符限制(140?我相信)以下,那么整个推特的文本将显示在文本类别中,没有任何问题,这正是我未来研究所需要的

但是,任何超过字符限制的推文在“文本”类别中显示如下:

@thedamon @getify I worry adding new terms add complexity and may make it harder for people to learn JavaScript. A… <url>StackOverflow不允许我显示下面的短URL,但本质上,正如我刚才所说,它是完整帖子的短twitter URL

如您所见,文本以“…”结尾,后跟链接。 要查看全文,我需要查看“extended_tweet”类别,然后将信息放在如下位置:

{'full_text': '@thedamon @getify I worry adding new terms add complexity and may make it harder for people to learn JavaScript. A sort function is a function you send to sort. Learning a new acronym to abstract that adds unnecessary complexity.', 'display_text_range': [18, 229], 'entities': {'hashtags': [], 'urls': [], 'user_mentions': [{'screen_name': 'thedamon', 'name': 'Damon Muma', 'id': 29938474, 'id_str': '29938474', 'indices': [0, 9]}, {'screen_name': 'getify', 'name': 'getify', 'id': 16686076, 'id_str': '16686076', 'indices': [10, 17]}], 'symbols': []}}

正如你所看到的,这比文本要详细得多

我目前正在使用Python,并试图用正则表达式来概括我的想法。我可以轻松地从索引[I]到索引[j]对字符串进行切片,但由于所有tweet的长度不同,我需要确保从tweet开始的那一点开始对tweet进行切片,即“全文”:&&;'显示\u文本\u范围'

我并不是要求别人帮我做作业,但我已经在这个问题上纠结了一段时间,我最初认为简单的事情结果比我想象的要困难得多

有没有人能给我一些建议或建议,让我自己解决这个问题

谢谢


Tags: toname文本刮板idnew字符类别
1条回答
网友
1楼 · 发布于 2024-10-03 11:21:55

为什么不解析JSON以获取full_text属性

import json

data = '''
{"full_text": "@thedamon @getify I worry adding new terms add complexity and may make it harder for people to learn JavaScript. A sort function is a function you send to sort. Learning a new acronym to abstract that adds unnecessary complexity.", "display_text_range": [18, 229], "entities": {"hashtags": [], "urls": [], "user_mentions": [{"screen_name": "thedamon", "name": "Damon Muma", "id": 29938474, "id_str": "29938474", "indices": [0, 9]}, {"screen_name": "getify", "name": "getify", "id": 16686076, "id_str": "16686076", "indices": [10, 17]}], "symbols": []}}'''

parsed_data = json.loads(data)
print(parsed_data['full_text']) # prints full tweet '@thedamon @getify I worry .... unnecessary complexity.'

相关问题 更多 >