是否可以对 HTML 类型提供程序中的表进行计数?
Is it possible to count tables in the HTML Type Provider?
我有一个 Wiki 页面,出于特定原因我有兴趣计算那里的表格。
显然,道具 Lists
和 Tables
的深处表示为序列:
有没有办法在代码中检索这些计数?
我试过几个可怕的技巧:
open System
open FSharp.Data
open FSharp.Data.Runtime
type Wiki = HtmlProvider<"https://en.wikipedia.org/wiki/F_Sharp_(programming_language)">
let getTablesCount (url : string) =
let data = Wiki.Load url
let tables = data.Tables
// won't compile - type constraint mismatch
// let attempt1 = tables :> Map<string, HtmlTable> |> Map.count
// won't compile - type is not compatible
// let attempt2 = tables |> Seq.cast<Tuple<string, HtmlTable>> |> Seq.length
// compiles - throws in the runtime InvalidCastException
// let attempt3 = (box tables) :?> Map<string, HtmlTable> |> Map.count
42
没有任何效果,可能永远有效。也许我遗漏了一些明显的东西?
我准备好 使用正则表达式解析 html 使用例如FSharp.Data HTML 解析器,只是想确定一下。
我对 HtmlProvider
不是很熟悉,我想你可以使用反射,也许可以得到非 public 类型,这很老套,或者使用 HtmlAgilityPack.
在搜索 "table" 节点的 HtmlProvider 中,我的计数为 10:
open FSharp.Data
type Wiki = HtmlProvider<"https://en.wikipedia.org/wiki/F_Sharp_(programming_language)">
[<EntryPoint>]
let main argv =
let getTablesCount (url : string) =
let data = Wiki.Load url
let tables = data.Tables
let props = tables.Html.Descendants("table")
props |> Seq.length |> (printfn "%A %A" "Table count is:")
getTablesCount("https://en.wikipedia.org/wiki/F_Sharp_(programming_language)")
0
我有一个 Wiki 页面,出于特定原因我有兴趣计算那里的表格。
显然,道具 Lists
和 Tables
的深处表示为序列:
有没有办法在代码中检索这些计数?
我试过几个可怕的技巧:
open System
open FSharp.Data
open FSharp.Data.Runtime
type Wiki = HtmlProvider<"https://en.wikipedia.org/wiki/F_Sharp_(programming_language)">
let getTablesCount (url : string) =
let data = Wiki.Load url
let tables = data.Tables
// won't compile - type constraint mismatch
// let attempt1 = tables :> Map<string, HtmlTable> |> Map.count
// won't compile - type is not compatible
// let attempt2 = tables |> Seq.cast<Tuple<string, HtmlTable>> |> Seq.length
// compiles - throws in the runtime InvalidCastException
// let attempt3 = (box tables) :?> Map<string, HtmlTable> |> Map.count
42
没有任何效果,可能永远有效。也许我遗漏了一些明显的东西?
我准备好 使用正则表达式解析 html 使用例如FSharp.Data HTML 解析器,只是想确定一下。
我对 HtmlProvider
不是很熟悉,我想你可以使用反射,也许可以得到非 public 类型,这很老套,或者使用 HtmlAgilityPack.
在搜索 "table" 节点的 HtmlProvider 中,我的计数为 10:
open FSharp.Data
type Wiki = HtmlProvider<"https://en.wikipedia.org/wiki/F_Sharp_(programming_language)">
[<EntryPoint>]
let main argv =
let getTablesCount (url : string) =
let data = Wiki.Load url
let tables = data.Tables
let props = tables.Html.Descendants("table")
props |> Seq.length |> (printfn "%A %A" "Table count is:")
getTablesCount("https://en.wikipedia.org/wiki/F_Sharp_(programming_language)")
0