F#-如何在标题上使用CSS选择器

时间:2019-03-28 09:19:47

标签: html css-selectors f#-data

我要使用CssSelect从每个表行中检索许多 span title 标记并将其写入csv文件。

<!DOCTYPE html>
<html>
<head>
    <title>F# CSS-Selector</title>
</head>
<body>
    <table id="tblId01" class="tblClss01">
        <tbody>
            <tr>
                <td class="tdClss01">
                    <span class="spnClss01">
                    <span title="A &amp; B">w x</span>
                    <span title="CD"><a href="/alpha" target="_blank">Alpha</a></span> + <span title="D"><a
                                    href="/bravo" target="_blank">Bravo</a></span>
                    <span title="E"><a href="/charlie" target="_blank">Charlie</a></span>
                    </span>
                </td>
            </tr>
            <tr>
                <td class="tdClss01">
                    <span class="spnClss01">
                    <span title="A &amp; B">y z</span>
                    <span title="CD"><a href="/delta" target="_blank">Delta</a></span> + <span title="D"><a
                                    href="/echo" target="_blank">Echo</a></span>
                    <span title="E"><a href="/foxtrot" target="_blank">Foxtrot</a></span>
                    </span>
                </td>
            </tr>
        </tbody>
    </table>
</body>
</html>

我目前的工作如下,但是不幸的是,代码正在遍历每一行的标题,但没有生成预期的输出,如下所示。

module SOQN = 

    open System
    open System.IO
    open FSharp.Data

    let url = @"C:\tmp\SOQN_CSS-Selector-Title_FS.html"
    let rslt = FSharp.Data.HtmlDocument.Load(url)

    let rcrd = 
      rslt.CssSelect(".tdClss01 > .spnClss01 > span[title]")
       |> List.map (fun a -> a.InnerText().Trim())

    let lists = [rcrd]

    let rec transpose xs =
        match xs with
        []::_ -> []
        | xs -> List.map List.head xs :: transpose (List.map List.tail xs)

    let output = transpose lists
                |> Array.ofList
                |> Array.map (fun x -> String.concat ";" x)

    printfn "%A" output

    let main() =
        printfn ""
        printfn "SOQN: How To Use CSS-Selector On Title?"
        printfn ""
        do 
            printfn "Saving to csv file..."
            use writer = new StreamWriter(@"C:\tmp\CSS-Selector-Title.csv")
            writer.WriteLine "A;B;C;D;E"
            output
            |> Seq.iter writer.WriteLine
        printfn ""
        printfn "Fini!"
        printfn ""
        0

    [<EntryPoint>]
    main() |> ignore

// Actual Output: 
// A;B;C;D;E
// w x
// Alpha
// Bravo
// Charlie
// y z
// Delta
// Echo
// Foxtrot

// Expected Output: 
// A;B;C;D;E
// w;x;Alpha;Bravo;Charlie
// y;z;Delta;Echo;Foxtrot
//

我想念什么?

0 个答案:

没有答案