【问题标题】:Trying to convert JSON to dictionary in python试图在python中将JSON转换为字典
【发布时间】:2020-08-08 12:44:19
【问题描述】:

我为 application/ld+json 抓取了一个网站,它返回 json,我想将字符串转换为 python 字典,但它似乎不起作用。在终端中,我从 None 收到错误 JSONDecodeError("Expecting value", s, err.value)。我对使用 JSON 比较陌生,所以我可能犯了一个愚蠢的错误,但是我在堆栈溢出中发现的所有内容都不起作用。任何帮助将不胜感激,感谢您抽出时间阅读我的帖子!

这是我的代码

from flask import Flask, render_template
from bs4 import BeautifulSoup
import requests
import json

source = requests.get('https://www.visionlearning.com/en/library/Chemistry/1/Nuclear-Chemistry/59').text
soup = BeautifulSoup(source, 'html.parser')

jsonString = str(soup.find_all('script', type='application/ld+json')[0])
print(json.loads(jsonString))

【问题讨论】:

    标签: python json beautifulsoup


    【解决方案1】:

    如果你打印出 jsonString 你会看到它包含<script> 标签,只需获取里面的内容:

    jsonString = str(soup.find_all('script', type='application/ld+json')[0].text)
    

    【讨论】:

    • 我是否需要为此导入一个库,因为当我打印(jsonString)时它只返回一个空行(没有错误消息)
    • 不,您不需要额外的库。如果你的 jsonString 是空的,试试把汤打印出来看看能不能正确获取网站内容。因为我刚刚再次运行它,它工作正常。
    • 是的,当我打印汤时,它会返回数据,但它确实有脚本标签,我认为 .text 会去掉这些标签。此外,当我输入 .text VS 代码时,它并没有显示为推荐的语法(希望有意义)
    • 我猜我们当时使用的是不同版本的 beautifulsoup。使用.string 而不是.text
    【解决方案2】:
    import requests
    from bs4 import BeautifulSoup
    import json
    
    
    def main(url):
        r = requests.get(url)
        soup = BeautifulSoup(r.content, 'html.parser')
        target = json.loads(soup.find("script").text)
        print(target.keys())
    
    
    main("https://www.visionlearning.com/en/library/Chemistry/1/Nuclear-Chemistry/59")
    

    输出:

    dict_keys(['@context', '@type', 'mainEntityOfPage', 'name', 'headline', 'author', 'datePublished', 'dateModified', 'image', 'publisher', 'description', 'keywords', 'inLanguage', 'copyrightHolder', 'copyrightYear'])
    

    【讨论】:

    • 我试过这个并从无收到相同的错误消息 JSONDecodeError("Expecting value", s, err.value)。我忘了导入安装的东西吗?
    • @Filtered 你使用的是哪个 Python 版本?
    • 3.8.2 ---------
    【解决方案3】:

    因为你得到了第一个值。您不必使用.find_all.find 将返回第一个值。使用.get_text.text 将其转换为字符串,然后将其转换为json。

    from bs4 import BeautifulSoup
    import requests
    import json
    
    source = requests.get('https://www.visionlearning.com/en/library/Chemistry/1/Nuclear-Chemistry/59').text
    soup = BeautifulSoup(source, 'html.parser')
    
    jsonString = soup.find('script', type='application/ld+json')
    
    print(json.loads(jsonString.get_text(strip=True)))
    

    【讨论】:

      【解决方案4】:

      这就是我在 jsonString 末尾添加 .contents[0] 的最终方法

      source = requests.get('https://www.visionlearning.com/en/library/Chemistry/1/Nuclear-Chemistry/59')
      soup = BeautifulSoup(source.content, 'html.parser')
      
      jsonString = soup.find_all('script', type='application/ld+json')[0].contents[0]
      print(json.loads(jsonString))
      

      感谢大家的帮助!

      【讨论】:

        猜你喜欢
        • 2021-01-17
        • 1970-01-01
        • 2011-01-29
        • 2019-04-19
        • 2017-07-02
        • 2014-02-26
        • 2018-05-04
        • 1970-01-01
        相关资源
        最近更新 更多