views:

1005

answers:

3

I'm trying to create a generalized HTML parser that works well on Blog Posts. I want to point my parser at the specific entrie's URL and get back clean text of the post itself. My basic approach (from python) has been to use a combination of BeautifulSoup / Urllib2, which is okay, but it assumes you know the proper tags for the blog entry. Does anyone have any better ideas?

Here are some thoughts maybe someone could expand upon, that I don't have enough knowledge/know-how yet to implement.

  1. The unix program 'lynx' seems to parse blog posts especially well - what parser do they use, or how could this be utilized?

  2. Are there any services/parsers that automatically remove junk ads, etc?

  3. In this case, i had a vague notion that it may be an okay assumption that blog posts are usually contained in a certain defining tag with class="entry" or something similar. Thus, it may be possible to create an algorithm that found the enclosing tags with the most clean text between them - any ideas on this?

Thanks!

+2  A: 

There are projects out there that specifically look at filtering out the 'noise' of a given page. Typically the way this is done is by giving the algorithm a few examples of a given type of page, and it can look at what parts don't change between them. That being said, you'd have to give the algorithm a few example pages/posts of every blog you wanted to parse. This usually works well when you have a small defined set of sites you'll be crawling (news sites, for instance). The algorithm is basically detecting the template they use in HTML and picking out the interesting part. There's no magic here, it's tough and imperfect.

A great example of this alogrithm can be found in the EveryBlock.com source code which was just open-sourced. Go to everyblock.com/code and download the "ebdata" package and look at the "templatemaker" module.

And I don't mean to state the obvious, but have you considered just using RSS from the blogs in question? Usually the fields have the entire blog post, title, and other meta info along with them. Using RSS is going to be far simpler than the previous solution I mentioned.

sotangochips
Yeah I actually have the RSS data, the problem is that many don't have the full text, and I need it in every case. Checking this out now, thanks.
+6  A: 

Boy, do I have the perfect solution for you.

Arc90's readability algorithm does exactly this. Given HTML content, it picks out the content of the main blog post text, ignoring headers, footers, navigation, etc.

Here are implementations in:

I'll be releasing a Perl port to CPAN in a couple of days. Done.

Hope this helps!

Anirvan
It turns out this worked really well - needed to make a few changes to make it better (change to SGML parser instead of HTMLParser in beautifulsoup), but what a great solution! Thanks
One quick note: Arc90's Readability tool has some weak spots. On a complex page like this one (http://blog.moertel.com/articles/2007/02/22/a-simple-directory-tree-printer-in-haskell), it silently drops most of the code blocks. That's a significant problem if you are going to use it to extract information from _coding_ blogs.
Telemachus
Thanks for the python and php links, I didn't know those existed.
Tristan Havelick
The PHP version seems fantastic. The link is broken. Here's a new one. http://www.keyvan.net/2010/08/php-readability/
Mridang Agarwalla
A: 

Hi Anirvan,

I tried the above code which is done on Python(dated Feb 22, 12.49). I get an error

Traceback (most recent call last):

File "C:\Users\workspace\secpython\src\hn.py", line 213, in (module)

print upgradeFeed(HN_RSS_FEED)

File "C:\Users\workspace\secpython\src\hn.py", line 180, in upgradeFeed

parsedFeed = feedparser.parse(feedData)

AttributeError: 'module' object has no attribute 'parse'

I am using Python 2.6.5 and Eclipse with Pydev(windows environment). Can you help me to solve this problem..

Thanks in Advance..