I had a similar problem trying to figure out how to use the FSharp.Data HtmlProvider
.
I am using Wikipedia to get information about USA presidents. The HtmlProvider
does a great job of discovering the various tables in that webpage, but I wanted to extract the logic for processing a row of "president data" into a separate function called processRow
.
And the problem was trying to work out what the type of such a row is for processRow
's parameter row
. The following code does the trick:
#load "Scripts\load-references.fsx"
open FSharp.Data
let presidents = new HtmlProvider<"https://en.wikipedia.org/wiki/List_of_Presidents_of_the_United_States">()
let ps = presidents.Tables.``List of presidents``
ps.Headers |> Option.map (fun hs -> for h in hs do printf "%s " h)
printfn ""
type Presidents = ``HtmlProvider,Sample="https://en.wikipedia.org/wiki/List_of_Presidents_of_the_United_States"``.ListOfPresidents
let processRow (row:Presidents.Row) =
printfn "%d %s" row.``№`` row.President2
ps.Rows |> Seq.iter processRow
I did not type in the long type alias for Presidents
, I used Visual Studio auto-completion by guessing that the type for List of presidents
would be discoverable from something starting with Html
, and it was, complete with the four single back quotes.