Parse HTML Code to Python nested dictionary

Hello everyone! I am new to this forum. I have got a problem I am stuck on. I want to create a python script that converts HTML Syntax to Python Dictionary.

Here is an example:

Input (HTML Code):


Output (Python Dictionary):

html = {
      "body": {
             "div": {
                   "img": {},
                   "button": {}

I am more interested in the algorithm step by step. Has someone any idea about how this can be done?

You can use a parser to do this: html.parser — Simple HTML and XHTML parser — Python 3.9.1 documentation

1 Like