fimad / scalpel

A high level web scraping library for Haskell.
Apache License 2.0
323 stars 43 forks source link

Select nth element? #63

Closed farao closed 6 years ago

farao commented 6 years ago

Hi,

in scalpel there some functions that simply return the "first match" - e.g. text or chroot. Is there a way to select/chroot the nth matching element? Suppose, I have the following HTML:

One

Two

Now I want to select only the second

- is there a way to do this?

Thanks, Marius

fimad commented 6 years ago

I would recommend fmaping drop over the all of the matches, and then pattern matching to pull out the first element:

scrapeStringLike "<div><p>One</p><p>Two</p></div>" $ do 
  a:_ <- drop 1 <$> texts "p"
  return a

Failing a pattern match inside the Scraper monad is safe because it is an instance of MonadFail.