如何在Python中使用json.loads获取文本

问题描述 投票:1回答:1

我写了一个从instagram获取用户名的代码。有时候我的算法不起作用,我的名字叫'p'。我试图为这个异常编写一个代码(在if head =='p':代码的一部分)。首先,我使用soup.select来获取这个信息块:

大段引用

{"@context":"http:\/\/schema.org","@type":"ImageObject","caption":"I think I\u2019m getting better at editing these.... and by that I mean that there getting more and more muddled to the point hat I don\u2019t think people will be able to tell what they are soon.... not really what I\u2019m going for but oh well.\n-\n\u2022September 3 2018\u2022\n-\n-\nThis is the update I mentioned on my cuts on my leg. I finally cleaned them after two days. I normally don\u2019t wait that long but didn\u2019t really have the right circumstances to actually get to clean them the night of the relapse. *shrug*\n-\n-\n-\n#selfharm #selfharmo","representativeOfPage":"http:\/\/schema.org\/True","uploadDate":"2018-09-04T06:27:24","author":{"@type":"Person",**"alternateName":"@alittlereddrop"**,"mainEntityofPage":{"@type":"ProfilePage","@id":"https:\/\/www.instagram.com\/alittlereddrop\/"}},"commentCount":"0","interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":"2"},"mainEntityofPage":{"@type":"ItemPage","@id":"https:\/\/www.instagram.com\/p\/BnS0sdDlsmP\/?tagged=selfharmo"},"description":"2 Likes, 0 Comments - No One Cares (@alittlereddrop) on Instagram: \u201cI think I\u2019m getting better at editing these.... and by that I mean that there getting more and more\u2026\u201d","name":"No One Cares on Instagram: \u201cI think I\u2019m getting better at editing these.... and by that I mean that there getting more and more muddled to the point hat I don\u2019t think\u2026\u201d"}

大段引用

有一个部分“alternateName”:包含一个名称。但即使使用json.loads我也无法得到它。你有什么想法?

file = open('users.txt', 'r', encoding="ISO-8859-1")
urls = file.readlines()
for url in urls:
url = url.strip ('\n')
try:
    req = requests.get(url)
    req.raise_for_status()
except HTTPError as http_err:
    output = open('output2.txt', 'a')
    output.write(f'К сожалению страница недоступна.\n')  
except Exception as err:
    output = open('output2.txt', 'a')
    output.write(f'К сожалению страница недоступна2\n')  
else:
    output = open('output2.txt', 'a')
    soup = BeautifulSoup(req.text, "lxml")
    the_url = soup.select("[rel='canonical']")[0]['href']
    the_url2=the_url.replace('https://www.instagram.com/','')
    head, sep, tail = the_url2.partition('/')
    if head == 'p':
        data = soup.select("[type='application/ld+json']")[0]
        oJson2 = json.loads(data.text)["alternateName"]
        str (oJson2)
        output.write (oJson2+'\n')
    else: 
        output.write (head+'\n')
python json python-3.x beautifulsoup instagram
1个回答
0
投票

您在json文件中遇到语法问题。在两个地方错误地放置了双星:

**"alternateName":"@alittlereddrop"**,

如果您要从文件中打开json,请执行以下操作:

import json

with open('yourfilename.json') as fo:
    jsn = json.loads(fo.read().replace('**', ''))
print(jsn['author']['alternateName'])
# '@alittlereddrop'

在您的情况下,尝试而不是这一行:

oJson2 = json.loads(data.text)["alternateName"]

这个

oJson2 = json.loads(data.text.replace('**', ''))['author']["alternateName"]
© www.soinside.com 2019 - 2024. All rights reserved.