Issue
How can I get the id
value from the following 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
}
Solution
The data you are seeing is in JSON format, you can convert it to a Python dictionary (dict
) using the built-in json
module, and then access the id
key:
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)
Output:
>>> print(type(json_data))
<class 'dict'>
>>> print(json_data["id"])
-2812448
Answered By - MendelG
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.