#๐Ÿ”’ Is this how I should properly parse HTML?

14 messages ยท Page 1 of 1 (latest)

mental bridge
#
#!/usr/bin/env python3

from html.parser import HTMLParser
import markdown
import re
import sys
import os

def esc(code):
    return f'\033[{code}m'

class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
           if tag == 'h1':
               print(esc(33) + '# ', end='')
           elif tag == 'h2':
               print(esc(34) + '## ', end='')
           elif tag == 'h3':
               print(esc(35) + '### ', end='')
           elif tag == 'a':
               print(esc('36;1;4'), end='')
           elif tag == 'li':
               print('- ', end='')
           else:
               print(esc(0), end='')

    def handle_endtag(self, tag):
           print(esc(0), end='')

    def handle_data(self, data):
           print(data, end='')

text = sys.stdin.read()
html = markdown.markdown(text)
parser = MyHTMLParser()
parser.feed(html)
print()
print()
parser.close()

Note: this is my first attempt at a post here.

I'm trying to understand how html parsing works via this little highlight script I'm tinkering with.
Is this how I should properly parse HTML, or is there another better way to do this? I'm currently following this guide: https://docs.python.org/3/library/html.parser.html.
This seems to work except that I want to add numbered links to the <a> tags. [3]link so it would display like this. So it seems calls between
method handlers would need to keep track of how many times it has been called.

For some reason I thought html parsing would convert html into json or a dict that I could iterate through.

How should I think about parsing html and am I doing this correctly?

analog tinselBOT
#

@mental bridge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

plush moss
#

Are you trying to make your own parser for learning purposes?
Or you want a html parser for a project?

mental bridge
#

just for learning purposes

daring ember
#

So it seems calls between
method handlers would need to keep track of how many times it has been called.
Define an attribute in __init__ and use/modify it in the methods

hybrid osprey
#
           match tag:
               case 'h1':
                   print(esc(33) + '# ', end='')
               case 'h2':
                   print(esc(34) + '## ', end='')
               case 'h3':
                   print(esc(35) + '### ', end='')
               case 'a':
                   print(esc('36;1;4'), end='')
               case 'li':
                   print('- ', end='')
               case _:
                   print(esc(0), end='')
daring leaf
#

lxml does just read the HTML as text and give you a tree object

hybrid osprey
#

won't lxml expect an XML document though, not HTML?

daring leaf
#

iirc beautifulsoup uses lxml under the hood

analog tinselBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.