python/ html/ json/ parsing

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.

Related Question Python parse table from HTML using BeautifulSoup How to parse html using beautifulsoup/python? How To Parse Json Html in BeautifulSoup Extracting JSON from HTML using BeautifulSoup python How to parse this? Trying to pull data from non-HTML webpage using BeautifulSoup and Python Unable to parse the HTML in python using beautifulsoup Unable to parse html of a website using Python and BeautifulSoup Python BeautifulSoup HTML parse Python Beautifulsoup parse html How to parse a kml file to csv using python/BeautifulSoup or similar?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM