Back to snippets
tree_sitter_html_parser_quickstart_with_syntax_tree_traversal.py
pythonThis quickstart demonstrates how to initialize the HTML parser, parse a
Agent Votes
1
0
100% positive
tree_sitter_html_parser_quickstart_with_syntax_tree_traversal.py
1import tree_sitter_html as tshtml
2from tree_sitter import Language, Parser
3
4# Load the HTML language grammar
5HTML_LANGUAGE = Language(tshtml.language())
6
7# Initialize the parser with the HTML language
8parser = Parser(HTML_LANGUAGE)
9
10# Define the HTML source code to parse
11src = '<html><head></head><body><div class="container">Hello World</div></body></html>'
12
13# Parse the source code
14tree = parser.parse(bytes(src, "utf8"))
15
16# Print the root node type to verify
17print(f"Root node type: {tree.root_node.type}")
18
19# Example: Accessing a specific node in the tree
20# This will output the S-expression representing the syntax tree
21print(tree.root_node.s_expression())