A recent blog entry by a Jeff Atwood says that you should never parse HTML using regular expressions - yet doesn't give an alternative.
i want to scrape search search results, extracting values:
<div class="used_result_container">
...
...
<div class="vehicleInfo">
...
...
<div class="makemodeltrim">
...
<a class="carlink" href="[Url]">[MakeAndModel]</a>
...
</div>
<div class="kilometers">[Kilometers]</div>
<div class="price">[Price]</div>
<div class="location">
<span class='locationText'>Location:</span>[Location]
</div>
...
...
</div>
...
...
</div>
...and it repeats
You can see the values i want to extract, [enclosed in brackets]:
- Url
- MakeAndModel
- Kilometers
- Price
- Location
Assuming we accept the premise that parsing HTML:
- generally a bad idea
- rapidly devolves into madness
What's the way to do it?
Assumptions:
- native Win32
- loose html
Assumption clarifications:
Native Win32
- .NET/CLR is not native Win32
- Java is not native Win32
- perl, python, ruby are not native Win32
- assume C++, in Visual Studio 2000, compiled into a native Win32 application
Native Win32 applications can call library code:
- copied source code
- DLLs containing function entry points
- DLLs containing COM objects
- DLLs containing COM objects that are COM-callable wrappers (CCW) around managed .NET objects
Loose HTML
- xml is not loose HTML
- xhtml is not loose HTML
- strict HTML is not loose HTML
Loose HTML implies that the HTML is not well-formed xml (strict HTML is not well-formed xml anyway), and so an XML parser cannot be used. In reality i was present the assumption that any HTML parser must be generous in the HTML it accepts.
Clarification#2
Assuming you like the idea of turning the HTML into a Document Object Model (DOM), how then do you access repeating structures of data? How would you walk a DOM tree? i need a DIV node that is a class of *used_result_container*, which has a child DIV of class of vehicleInfo. But the nodes don't necessarily have to be direct children of one another.
It sounds like i'm trading one set of regular expression problems for another. If they change the structure of the HTML, i will have to re-write my code to match - as i would with regular expressions. And assuming we want to avoid those problems, because those are the problems with regular expressions, what do i do instead?
And would i not be writing a regular expression parser for DOM nodes? i'm writing an engine to parse a string of objects, using an internal state machine and forward and back capture. No, there must be a better way - the way that Jeff alluded to.
i intentionally kept the original question vague, so as not to lead people down the wrong path. i didn't want to imply that the solution, necessarily, had anything to do with:
- walking a DOM tree
- xpath queries
Clarification#3
The sample HTML i provided i trimmed down to the important elements and attributes. The mechanism i used to trim the HTML down was based on my internal bias that uses regular expressions. i naturally think that i need various "sign-posts in the HTML that i look for.
So don't confuse the presented HTML for the entire HTML. Perhaps some other solution depends on the presence of all the original HTML.
Update 4
The only propsed solutions seem to involve using a library to convert the HTML into a Document Object Model (DOM). The question then would have to become: then what?
Now that i have the DOM, what do i do with it? It seems that i still have to walk the tree with some sort of regular DOM expression parser, capable of forward matching and capture.
In this particular case i need all the *used_result_container* DIV nodes which contain vehicleInfo DIV nodes as children. Any *used_result_container* DIV nodes that do not contain vehicleInfo has a child are not relavent.
Is there a DOM regular expression parser with capture and forward matching? i don't think XPath can select higher level nodes based on criteria of lower level nodes:
\\div[@class="used_result_container" && .\div[@class="vehicleInfo"]]\*
Note: i use XPath so infrequently that i cannot make up hypothetical xpath syntax very goodly.