Parsing html with Haskell

Roy wrote himself some Ruby code to parse Wikipedia edits because he was being too much of a geek to pay attention to the Redskins game. That's all fine and dandy, except he then put out an invitation for someone to post a Haskell version -- damn him.

Next thing I know I'm looking around for a decent html parser in Haskell. The best I could find was the latest darcs version of TagSoup by Neil Mitchell. It works better than the released 0.1 version -- but there looks to be some room for improvement as its not quite as easy to use as the Hpricot library in Ruby.

Well, no worries though, it gets the job done. See the code below in all its Haskellific glory.

Wikipedia actually checks the User-Agent headers to verify if it wants to allow access to the edits page so the save function won't really work with the site. But rather than clutter up this code example with using the http library in Haskell, I opted to write the code to read from a cache file. Hit the wikipedia url in the code with your browser and save it to a file. Then run the code on it.

If everything worked, the output will be the editor name, a colon and their comment in parentheses. Blank comments get a default "no comment" filler.

 Angus Lepper: (Reverted 1 edit by 163.248.212.36 identified as vandalism to last  revision by Staecker. using TW)
  1. (no comment)Staecker: (Reverted edits by 87.81.110.133 (talk) to last version by Ruakh)
  2. (no comment)Ruakh: (→Examples - typofix)
  3. (→Examples)SieBot: (robot Adding: bg:Haskell) Eklitzke: (add gofer as a dialect of haskell)
  4. (→See also - + Curry (programming language))Tobias Bergemann: (→Examples - Linked to function composition operator)
  5. (no comment)
  6. (added Hungarian wiki version)Ruakh: (Undid vandalism by 129.16.25.16 (talk))
  7. (no comment)Ruakh: (it doesn't make sense to say that Gofer influenced Haskell)
  8. (→History)

tags: haskell

Comments

comments powered by Disqus