gpt4 book ai didi

Python - 从文件中提取文本实例

转载 作者:行者123 更新时间:2023-12-01 08:11:35 24 4
gpt4 key购买 nike

我是 python 的初学者,我正在开发一个项目,该项目可以解析 Instagram 帖子中的评论以确定谁先发表评论。除了解析从 Instagram API 提取评论时获得的大型文本文件之外,一切都正常。我尝试使用 split 语句按“id”的实例进行分割,但这并没有达到我想要的效果,而且我似乎不知道如何做到这一点。

我试图解析的文本文件在这里:https://pastebin.com/SjyaUZ1u

代码如下

with open("comments.txt", "r") as f:
for line in f:
str = line.split("id", 10)
print(str)

最终我想将这些实例与以下文本一起输出到另一个文件中,但这是现在的主要问题。

最佳答案

  • 使用 BeautifulSoup 及其 html.parser 解析您的 html
  • 找到pre标签内的json内容
  • 将其转换为字典格式
  • 使用键获取数据。
<小时/>
from bs4 import BeautifulSoup
import json
content = open('comments.txt')

soup = BeautifulSoup(content, 'html.parser')

a = soup.find('pre').get_text()

# print(a)

data = json.loads(a)
# print(data['graphql']['shortcode_media']['edge_media_to_comment']['edges'])
comments = data['graphql']['shortcode_media']['edge_media_to_comment']['edges']

for i in comments:
print(i)

输出:

{'node': {'id': '17866050361316207', 'text': '@pay2on gimme that coin', 'created_at': 1549838809, 'did_report_as_spam': False, 'owner': {'id': '351807810', 'is_verified': False, 'profile_pic_url': 'https://scontent-dfw5-1.cdninstagram.com/vp/3d9c34d98843652774cbe4ed24111890/5CDB0369/t51.2885-19/s150x150/46791596_1837989999663259_8814990427338833920_n.jpg?_nc_ht=scontent-dfw5-1.cdninstagram.com', 'username': 'pay2on'}, 'viewer_has_liked': False, 'edge_liked_by': {'count': 0}}}
{'node': {'id': '18015390769112027', 'text': 'Gg', 'created_at': 1549838810, 'did_report_as_spam': False, 'owner': {'id': '1620477479', 'is_verified': False, 'profile_pic_url': 'https://scontent-dfw5-1.cdninstagram.com/vp/386991ecd3581167df6fbcdcc3fbffba/5CDA79E7/t51.2885-19/s150x150/43093738_486110321900668_5555852948304560128_n.jpg?_nc_ht=scontent-dfw5-1.cdninstagram.com', 'username': 'a_andrestares'}, 'viewer_has_liked': False, 'edge_liked_by': {'count': 0}}}
{'node': {'id': '18032678767050139', 'text': '💀', 'created_at': 1549838821, 'did_report_as_spam': False, 'owner': {'id': '10336797539', 'is_verified': False, 'profile_pic_url': 'https://scontent-dfw5-1.cdninstagram.com/vp/a76a0efe6e172d370cd1e78d91fd0dd8/5CF3F51A/t51.2885-19/50496250_242828456650749_2095120626497880064_n.jpg?_nc_ht=scontent-dfw5-1.cdninstagram.com', 'username': 'sourpods'}, 'viewer_has_liked': False, 'edge_liked_by': {'count': 0}}}
{'node': {'id': '18031342408039828', 'text': '@pay2on anticoin', 'created_at': 1549838844, 'did_report_as_spam': False, 'owner': {'id': '4726002591', 'is_verified': False, 'profile_pic_url': 'https://scontent-dfw5-1.cdninstagram.com/vp/124d070772b728492cb4a2aa00ff7919/5D01194C/t51.2885-19/s150x150/49303395_379094076216927_6321318684270788608_n.jpg?_nc_ht=scontent-dfw5-1.cdninstagram.com', 'username': 'anti.tv'}, 'viewer_has_liked': False, 'edge_liked_by': {'count': 0}}}
{'node': {'id': '17900497066291896', 'text': 'H9', 'created_at': 1549838912, 'did_report_as_spam': False, 'owner': {'id': '9667789306', 'is_verified': False, 'profile_pic_url': 'https://scontent-dfw5-1.cdninstagram.com/vp/de6dfba0978218656579b080ee53f8c4/5CE83473/t51.2885-19/s150x150/50746304_2379848008910407_7742041896683307008_n.jpg?_nc_ht=scontent-dfw5-1.cdninstagram.com', 'username': 'chronic_wanna_be'}, 'viewer_has_liked': False, 'edge_liked_by': {'count': 0}}}

关于Python - 从文件中提取文本实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55214974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com