Is Haskell's Network.Browser module like Perl's LWP or Python's mechanize?
The Network.Browser documentation says that the module supports the following:
- HTTP Authentication handling
- Transparent handling of redirects
- Cookie stores + transmission.
- Transaction logging Proxy-mediated connections.
To me, it sounds like the beginnings of a browser that lets me crawl web pages, handling authenciation into web sites, cookies, etc.
However, the module comes with zero example code, instructions or tutorials. I cannot figure out how to use it.
Can开发者_开发技巧 someone give an example of how it might be used to 1) go to a web site, 2) log into it, and 3) download a file that requires you to be logged in?
I'd suggest to take a look at Network.Curl as well.
To answer your question, here is an example (taken from http://haskell.pastebin.com/9kPiGxiH):
import Data.IORef
import Network.HTTP
import Network.Browser
import Network.URI
import Data.Maybe
import Control.Monad
import Data.List
import Text.Regex.TDFA
import Control.Concurrent
pageUrl off = URI "http:" (Just $ URIAuth "" "www.interpals.net" "") "/dosearch.php" ("?todo=search&sec=adv&age1=15&age2=18&sex[]=FEMALE&lfor[]=lfor_email&lfor[]=lfor_snail&lfor[]=lfor_langex&lfor[]=lfor_friend&lfor[]=lfor_flirt&lfor[]=lfor_relation&countries[]=AT&countries[]=DE&countries[]=CH&state=&languages[]=any&keywords=&sort=p.last_login+DESC&offset="++(show off)) ""
getPage :: URI -> BrowserAction (HandleStream [Char]) String
getPage uri = do
setErrHandler $ const $ return ()
setOutHandler $ const $ return ()
(_,s) <- request $ Request (uri) GET
[Header HdrCookie "__ubic1=MTE3ODM0NDM0MTRjN2RkYTA1OTAzMmU4LjkxODE1Njk2; __utma=46363135.421215970.1283316265.1283538085.1283541700.10; __utmz=46363135.1283316265.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none); __utmc=46363135; PHPSESSID=59a130c66d4853f85289852f15cefa3a; resolution=1920x1080; ip_auto_login[login]=cap11235; ip_auto_login[password_md5]=NDM0NWM0NDlkZTg4MjRkMWVhZmJmZWNiZTQwOWQ4YTE%3D; __utmb=46363135"] ""
return $ rspBody s
getPeople :: Int -> BrowserAction (HandleStream [Char]) ([String], Int)
getPeople off = do
s <- getPage (pageUrl off)
let t = (s=~"<a href='/([^?.]+)\\?")::[[String]]
let next = if length t > 0 then off+10 else 0
return (nub $ map (!!1) t, next)
personUrl :: String -> URI
personUrl name = fromJust $ parseURI ("http://www.interpals.net/"++name)
viewPerson :: String -> BrowserAction (HandleStream [Char]) ()
viewPerson name = do
_ <- getPage $ personUrl name
return ()
doCycle :: IORef (Int, Int) -> IO ()
doCycle r = do
(count, off) <- readIORef r
(people, newOff) <- browse $ getPeople off
mapM_ (forkIO . browse . viewPerson) people
let newCount = count + (length people)
writeIORef r (newCount, if newOff<2000 then newOff else 0)
print newCount
doCycle r
main = do
t <- newIORef (0,0)
doCycle t
精彩评论