首页 > 解决方案 > 如何使用 BeautifulSoup 获得“id”值?

问题描述

如何id从以下 HTML 中获取值?

print(type(author_info))
output: <class 'bs4.element.Tag'>



print(author_info)
output: <script data-mru-fragment="models/user/journal" type="text/plain">
    {
        "name": "on-line журнал РАЗНЫЕ ЛЮДИ",
        "id": "-2812448",
        "auId": "8911662942803793376",
        "email": "rl_journal",
        "dir": "/community/rl_journal/",
    


    "isVip": false,
    "isCommunity": true,
    "isVideoChannel": false
}

标签: pythonparsingbeautifulsoupkey

解决方案


您看到的数据是JSONdict格式,您可以使用内置模块将其转换为 Python 字典 ( ) json,然后访问id密钥:

import json
from bs4 import BeautifulSoup

script_doc = """
<script data-mru-fragment="models/user/journal" type="text/plain">
    {
        "name": "on-line журнал РАЗНЫЕ ЛЮДИ",
        "id": "-2812448",
        "auId": "8911662942803793376",
        "email": "rl_journal",
        "dir": "/community/rl_journal/",
        "isVip": false,
        "isCommunity": true,
        "isVideoChannel": false
}</script>"""
soup = BeautifulSoup(script_doc, "html.parser")

json_data = json.loads(soup.find("script").string)
# With your example using `author_info`:
# json_data = json.loads(author_info.string)

输出:

>>> print(type(json_data))
<class 'dict'>

>>> print(json_data["id"])
-2812448

推荐阅读