Web Scraping With Haskell

HaskellHtml ParsingWeb Scraping

Haskell Problem Overview


What is the current state of libraries for scraping websites with Haskell?

I'm trying to make myself do more of my quick oneoff tasks in Haskell, in order to help increase my comfort level with the language.

In Python, I tend to use the excellent PyQuery library for this. Is there something similarly simple and easy in Haskell? I've looked into Tag Soup, and while the parser itself seems nice, actually traversing pages doesn't seem as nice as it is in other languages.

Is there a better option out there?

Haskell Solutions


Solution 1 - Haskell

http://hackage.haskell.org/package/shpider

> Shpider is a web automation library > for Haskell. It allows you to quickly > write crawlers, and for simple cases ( > like following links ) even without > reading the page source. > > It has useful features such as turning > relative links from a page into > absolute links, options to authorize > transactions only on a given domain, > and the option to only download html > documents. > > It also provides a nice syntax for > filling out forms. > > An example:

 runShpider $ do
      download "http://apage.com"
      theForm : _ <- getFormsByAction "http://anotherpage.com"
      sendForm $ fillOutForm theForm $ pairs $ do
            "occupation" =: "unemployed Haskell programmer"
            "location" =: "mother's house"

(Edit in 2018 -- shpider is deprecated, these days https://hackage.haskell.org/package/scalpel might be a good replacement)

Solution 2 - Haskell

From my searching on the Haskell mailing lists, it appears that TagSoup is the dominant choice for parsing pages. For example: http://www.haskell.org/pipermail/haskell-cafe/2008-August/045721.html

As far as the other aspects of web scraping (such as crawling, spidering, and caching), I searched http://hackage.haskell.org/package/ for those keywords but didn't find anything promising. I even skimmed through packages mentioning "http" but nothing jumped out at me.

Note: I'm not a regular Haskeller, so I hope others can chime in if I missed something.

Solution 3 - Haskell

Although I'm still for now a beginner in Haskell, I have the strong opinion that HTML parsing in 2012 must be done using CSS selectors, and it seems the libraries recommended so far don't use that principle.

One possibility is HandsomeSoup, which is built on top of HXT:

http://egonschiele.github.com/HandsomeSoup/

http://codingtales.com/2012/04/25/scraping-html-with-handsomesoup-in-haskell

This page about HXT, on which HandsomeSoup relies, will also be helpful (you're going to need getText or deep getText):

http://adit.io/posts/2012-04-14-working_with_HTML_in_haskell.html

But another choice is dom-selector:

http://hackage.haskell.org/package/dom-selector

It is right now alpha and its long-term maintenance could be a problem. The advantage of dom-selector is that I couldn't get unicode characters to work with HandsomeSoup. They worked out of the box with dom-selector.

This question is related to that: https://stackoverflow.com/questions/11382211/is-it-possible-to-use-text-or-bytestring-on-hxt-in-haskell

dom-selector is based on html-conduit and xml-conduit, for which maintenance appears assured.

EDIT: note my newer answer about lens-based parsing. I left this answer as it's still good on its own, but I would now personally rather use the other approach.

Solution 4 - Haskell

I wrote another answer to this question already, suggesting CSS selectors-based parsing, however that answer is now a year and a half old, and nowadays I think lenses might be a better approach in haskell. In effect you get something like type-safe compiled selectors.

See this reddit discussion for a couple of options in that vein. In case the link disappears, I copy the direct links:

I have used none of those yet, but if I would write new code parsing HTML today, I would definitely go with a lens-based approach.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionricreeView Question on Stackoverflow
Solution 1 - HaskellsclvView Answer on Stackoverflow
Solution 2 - HaskellDavid J.View Answer on Stackoverflow
Solution 3 - HaskellEmmanuel TouzeryView Answer on Stackoverflow
Solution 4 - HaskellEmmanuel TouzeryView Answer on Stackoverflow