0

I've heard, that parsing HTML using the Cthulhu way is not very good. But what are the right ways to parse HTML? Or is it possible to parse it at all?

closed as unclear what you're asking by gnat, Pieter B, Bart van Ingen Schenau, World Engineer Apr 15 '14 at 14:11

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Seeing as browsers parse HTML... what do you say? Is it possible? – Oded Apr 15 '14 at 9:04
  • 2
    Unclear what help you need. Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it’s hard to tell what problem you are trying to solve or what aspect of your approach needs to be corrected or explained. See the How to Ask page for help clarifying this question. – gnat Apr 15 '14 at 9:04
  • 2
    By using a... parser? – Oliver Weiler Apr 15 '14 at 9:05
4

Or is it possible to parse it at all?

Some say it's possible, and that even webbrowsers use this feature to display web pages.

what are the right ways to parse HTML?

Basically you need a parser able to express the idea that an html element can be composed of other html elements.

<div>
  some text 
  <div>
    nested element!!
  </div> <!--a regular expression cannot tell if this closes the first or second div-->
</div>

This cannot be done with regular expression. But you can do it with more general kinds of parsers.

see https://stackoverflow.com/questions/590747/using-regular-expressions-to-parse-html-why-not

  • 1
    Your answer forgets that “regular expressions” (the computer science thing) and “regular expressions” (regex engines like PCRE) are very different, and that it's perfectly possible to parse any non left recursive context-free language with PCRE or equivalent. So it's entirely possible to write a HTML parser with regexes, it's just extremely difficult to do correctly, so you're better off using an existing HTML parser rather than using a half-assed regex that fails on unexpected but valid input. – amon Apr 15 '14 at 9:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.