HTML Parsing - Get the freshest HTML tags
When I'm parsing HTML, I only want to get the innermost tags for the entire document. My intention is to semantically parse data from an HTML document.
So if I have html like this
<html>
<table>
<tr><td>X</td></tr>
<tr><td>Y</td></tr>
</table>
</html>
I only want <td>X</td>
and <td>Y</td>
. Is this possible using Beautiful Soup or lxml?
+1
source to share
4 answers
In .NET I used HtmlAgilityPack to make all html syntax easy. It loads the DOM and you can select by nodes, in your case select nodes with no children. Perhaps it helps.
+3
source to share
If you can use or manipulate the DOM (i.e. in the browser), you can work with the parentNode attribute for all tags and recursively count the total and keep the largest.
In javascript pseudocode (tested on FireFox):
var allElements = document.getElementsByTagName("*");
var maxElementReference, maxParentNodeCount = 0;
var i;
for (i = 0; i < allElements.length; i++) {
var count = recursiveCountParentNodeOn(allElements[i]);
if (maxParentNodeCount < count) {
maxElementReference = allElements[i];
maxParentNodeCount = count;
}
}
0
source to share