import Network.HTTP import Text.Parsec data YahooSearchItem = YahooSearchItem { itemUrl, itemTitle, itemContent :: String } data YahooSearch = YahooSearch { searchQuery :: String, searchPage :: Int, searchItems :: [YahooSearchItem] } -- URL for Yahoo! searches, without giving a page number yahooUrl = "http://search.yahoo.com/search?p=" -- make an HTTP request and return a YahooSearch yahoo :: String -> IO YahooSearch yahoo q = simpleHTTP (getRequest $ yahooUrl ++ q) >>= getResponseBody >>= return . YahooSearch q 1 . items -- get some results and return the next page of results next :: YahooSearch -> IO YahooSearch next (YahooSearch q p _) = simpleHTTP (getRequest $ -- add the page number to the search yahooUrl ++ q ++ "&b=" ++ show (p + 1)) >>= getResponseBody >>= return . YahooSearch q (p + 1) . items printResults :: YahooSearch -> IO () printResults (YahooSearch q p items) = do putStrLn $ "Showing Yahoo! search results for query: " ++ q putStrLn $ "Page: " ++ show p putChar '\n' mapM_ printOne items where printOne (YahooSearchItem itemUrl itemTitle itemContent) = do putStrLn $ "URL : " ++ itemUrl putStrLn $ "Title : " ++ itemTitle putStrLn $ "Abstr : " ++ itemContent putChar '\n' urlTag, titleTag, contentTag1, contentTag2, ignoreTag, ignoreText :: Parsec String () String -- parse a tag containing the URL of a search result urlTag = do { string "'); return url } -- the title comes after the URL tag, so parse it first, discard it -- and get the title text titleTag = do { urlTag; manyTill anyChar (try (string "")) } -- parse a tag containing the description of the search result -- the tag can be named "sm-abs" or "abstr" contentTag1 = do { string "