I have an HTML page with various information, like:
<input class="json-data" id="init-data" type="hidden" value='{"keyboardShortcuts":[{"name":"xxxx","description":"yyyyy", > etc...
How can I obtain something like
name = xxxx
That is my code so far, but every time it just prints None:
content = page.read()
html = BeautifulSoup(content, "html.parser")
element = html.find("input", class_="json-data", value_="keyboardShortcuts")
Pull out the value
attribute, read using json
, then you can get the value for the name
key:
from bs4 import BeautifulSoup
import json
content = '''
<input class="json-data" id="init-data" type="hidden" value='{"keyboardShortcuts":[{"name":"xxxx","description":"yyyyy"}]}
'''
html = BeautifulSoup(content, "lxml")
jsonData = json.loads(html.find('input', {'id':'init-data'})['value'])
print (jsonData['keyboardShortcuts'][0]['name'])
Output:
print (jsonData['keyboardShortcuts'][0]['name'])
xxxx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.