【发布时间】:2016-08-04 22:05:13
【问题描述】:
我是 F# 的新手,所以如果我使用了不正确的名称,我深表歉意。
我正在尝试使用 F# 来解析一个看起来像这样的网页:
<!--This is simplified, in reality there are more divs, anchors etc. -->
<html>
<body>
<div class="pr-single"><a href="http://google.ca">Google</a></div>
<div class="pr-single"><a href="http://apple.com">Apple</a></div>
<div class="pr-single"><a href="http://microsoft.com">Microsoft</a></div>
</body>
</html>
我已经声明了一个类型
type PromoterPage = FSharp.Data.HtmlProvider<"http://somewebpage.com">
现在我正在尝试获取页面上所有链接的列表。我的想法是:
- 按类名获取所有外层div
- 获取所有这些 div 的后代
- 将这些后代收集到一个平面列表中
- 将此列表过滤为仅
<a>标签
我的尝试如下:
let GetFirst (page:PromoterPage) =
page.Html.Descendants()
|> Seq.filter(fun n -> n.HasClass("pr-single")) //Find the divs
|> Seq.map(fun n -> n.Descendants()) //Get the descendants
|> Seq.collect(fun n -> n |> Seq.where(fun m -> m.HasName("a")) //Filter and collect the anchors
问题似乎是你不能嵌套Seq 函数或者我做的不正确。我收到错误:
Incomplete values or function definition. If this is an expression, the body of the expression must be indented to the same column as the keyword.
我可以按照我在这里尝试的方式嵌套Seq 函数吗?我是否以错误的方式思考这个问题?
【问题讨论】: