【问题标题】:Parsing a JSON file with BeautifulSoup使用 BeautifulSoup 解析 JSON 文件
【发布时间】:2016-10-02 10:27:39
【问题描述】:

对编码相当陌生,所以对于任何基本错误请多多包涵。简化,我有这个:

url = "https://www.youtube.com/browse_ajax?action_continuation=1&continuation=4qmFsgIuEiRWTFBMbFRwZUhIS2o1TFNpRVpKbXJxZGg0MHlqZjBmdG4xak4aBkNHVSUzRA%253D%253D"

lm_json = requests.get(url).json()

我将如何使用 BeautifulSoup 解析 lm_json?我不断收到错误以将其作为文件打开。我需要使用 BeautifulSoup,因为我在 while 循环中使用结果。

从这一步开始,我需要将它附加到主汤中。

感谢您的帮助!

【问题讨论】:

    标签: python html json beautifulsoup


    【解决方案1】:

    HTML 内容包装在 JSON 中。对于您的示例,您可以尝试:

    url = "https://www.youtube.com/browse_ajax?action_continuation=1&continuation=4qmFsgIuEiRWTFBMbFRwZUhIS2o1TFNpRVpKbXJxZGg0MHlqZjBmdG4xak4aBkNHVSUzRA%253D%253D"
    lm_json = requests.get(url).json()
    soup = BeautifulSoup(lm_json["content_html"])
    

    【讨论】:

    • 我收到AttributeError: 'dict' object has no attribute 'content_html' 编辑:刚刚尝试lm_soup = BeautifulSoup(lm_json['content_html']),它似乎解析正确
    猜你喜欢
    • 1970-01-01
    • 2020-03-19
    • 2020-11-22
    • 1970-01-01
    • 2022-01-26
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    相关资源
    最近更新 更多