I have some random HTML layouts that contain important text I would like to extract. I cannot just strip_tags() as that will leave a bunch of extra junk from the sidebar/footer/header/etc.
I found a method built in Python and I was wondering if there is anything like this in PHP.
The concept is rather simple: use
information about the density of text
vs. HTML code to work out if a line of
text is worth outputting. (This isn’t
a novel idea, but it works!) The basic
process works as follows:
- Parse the HTML code and keep track of the number of bytes processed.
- Store the text output on a per-line, or per-paragraph basis.
- Associate with each text line the number of bytes of HTML required to
describe it.- Compute the text density of each line by calculating the ratio of text
t> o bytes.- Then decide if the line is part of the content by using a neural network.
You can get pretty good results just
by checking if the line’s density is
above a fixed threshold (or the
average), but the system makes fewer
mistakes if you use machine learning –
not to mention that it’s easier to
implement!
Update: I started a bounty for an answer that could pull main content from a random HTML template. Since I can’t share the documents I will be using – just pick any random blog sites and try to extract the body text from the layout. Remember that the header, sidebar(s), and footer may contain text also. See the link above for ideas.
UPDATE 2
#sidebar, #header, #footer, #comments, etc..script, iframe/\d+\scomment(?:[s])/im/(read the rest|read more).*/im/(?:.*(?:by|post|submitt?)(?:ed)?.*\s(at|am|pm))/im/[^a-z0-9]+/imsearch for well know classes and ids:
.entry-content.post-entry .entry .post.post.post-body .entry-content.content.post.journal-entry-text.entrygawker.com
.post-bodyRef: The blog platforms of choice among the top 100 blogs
search based on common html structure that look like this: