我是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)
最终,我想将这些实例以及后面的文本输出到另一个文件中,但这是当前的主要问题。
答案 0 :(得分:1)
BeautifulSoup
及其html.parser
解析您的html json
标记内找到pre
内容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}}}