-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This commit adds the resolutions to #23 and #25 to README.md under a new tips & tricks section. Full source code for each is given in a new example as well. Additionally a backtracking bug was found in `chroot` while documenting these tricks and is fixed in this commit as well.
- Loading branch information
Showing
7 changed files
with
215 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import Text.HTML.Scalpel | ||
import Control.Applicative | ||
import Control.Monad | ||
import Data.List (isInfixOf) | ||
|
||
|
||
exampleHtml :: String | ||
exampleHtml = "<html>\ | ||
\ <body>\ | ||
\ <div class='comments'>\ | ||
\ <div class='comment container'>\ | ||
\ <span class='comment author'>Sally</span>\ | ||
\ <div class='comment text'>Woo hoo!</div>\ | ||
\ </div>\ | ||
\ <div class='comment container'>\ | ||
\ <span class='comment author'>Bill</span>\ | ||
\ <img class='comment image' src='http://example.com/cat.gif' />\ | ||
\ </div>\ | ||
\ <div class='comment container'>\ | ||
\ <span class='comment author'>Bertrand</span>\ | ||
\ <div class='comment text'>That sure is some cat!</div>\ | ||
\ </div>\ | ||
\ <div class='comment container'>\ | ||
\ <span class='comment author'>Susan</span>\ | ||
\ <div class='comment text'>WTF!?!</div>\ | ||
\ </div>\ | ||
\ </div>\ | ||
\ </body>\ | ||
\</html>" | ||
|
||
main :: IO () | ||
main = print $ scrapeStringLike exampleHtml catComment | ||
|
||
catComment :: Scraper String String | ||
catComment = | ||
-- 1. First narrow the current context to the div containing the comment's | ||
-- textual content. | ||
chroot ("div" @: [hasClass "comment", hasClass "text"]) $ do | ||
-- 2. Any can be used to access the root tag of the current context. | ||
contents <- text Any | ||
-- 3. Skip comment divs that do not contain "cat". | ||
guard ("cat" `isInfixOf` contents) | ||
-- 4. Generate the desired value. | ||
html Any |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Text.HTML.Scalpel | ||
|
||
|
||
exampleHtml :: String | ||
exampleHtml = "<html>\ | ||
\ <body>\ | ||
\ <div class='comments'>\ | ||
\ <div class='comment container'>\ | ||
\ <span class='comment author'>Sally</span>\ | ||
\ <div class='comment text'>Woo hoo!</div>\ | ||
\ </div>\ | ||
\ <div class='comment container'>\ | ||
\ <span class='comment author'>Bill</span>\ | ||
\ <img alt='A cat picture.' \ | ||
\ class='comment image' src='http://example.com/cat.gif' />\ | ||
\ </div>\ | ||
\ <div class='comment container'>\ | ||
\ <span class='comment author'>Susan</span>\ | ||
\ <div class='comment text'>WTF!?!</div>\ | ||
\ </div>\ | ||
\ <div class='comment container'>\ | ||
\ <span class='comment author'>Bill</span>\ | ||
\ <img alt='A dog picture.' \ | ||
\ class='comment image' src='http://example.com/dog.gif' />\ | ||
\ </div>\ | ||
\ </div>\ | ||
\ </body>\ | ||
\</html>" | ||
|
||
main :: IO () | ||
main = print $ scrapeStringLike exampleHtml altTextAndImages | ||
|
||
altTextAndImages :: Scraper String [(String, URL)] | ||
altTextAndImages = | ||
-- 1. First narrow the current context to each img tag. | ||
chroots "img" $ do | ||
-- 2. Use Any to access all the relevant content from the the currently | ||
-- selected img tag. | ||
altText <- attr "alt" Any | ||
srcUrl <- attr "src" Any | ||
-- 3. Combine the retrieved content into the desired final result. | ||
return (altText, srcUrl) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters