[erlang-questions] page counter - thx for everyones help
Wes James
comptekki@REDACTED
Sat May 1 00:47:53 CEST 2010
On Fri, Apr 30, 2010 at 4:17 PM, Colm Dougan <colm.dougan@REDACTED> wrote:
> Wes,
>
> It would be typical to move the "extract page count" code into a
> function. Depending on how happy you are depend on the format of the
> page you are scraping you could use something like this (see below).
> Note: I've used the default "http" library here rather than "ibrowse"
> as I don't have ibrowse installed. If you decide to use "re" instead
> then you only have to change the extract_page_count function.
>
> Typically lists:nth isn't very erlangy. It would probably be more
> typical (and more tolerant to the page changing) to iterate over Lines
> looking for a given pattern.
>
> Colm
>
> -define(MAGIC_LINE_NUMBER, 424).
> -define(URL, ""http://192.168.1.1/printer/configu.html?autoref=0&weblang=0").
>
> getc() ->
> inets:start(),
> {ok, {_StatusLine, _Headers, Body}} = http:request(?URL),
> Lines = string:tokens(Body, "\r\n"),
> PageCount = extract_page_count(lists:nth(?MAGIC_LINE_NUMBER, Lines)),
> io:format("COUNT [~p]~n", [PageCount]),
> ok.
>
> extract_page_count("<B>Page Counter</B></TD><TD>" ++ Rest) ->
> {Count, _} = string:to_integer(Rest),
> Count.
Colm,
Thx! I like this a lot better without having to use ibrowse.
I appreciate this info!
-wes
More information about the erlang-questions
mailing list