【发布时间】:2011-03-18 03:30:59
【问题描述】:
我正在尝试使用 html 敏捷性从一些 html 中解析一些数据。这是包含许多表行的数据:
<tr>
<td><a href="showindex.cfm"><span class="style1">companies</span></a></td>
<td><b>71</b></td>
</tr>
<tr>
<td>
<font><b><a href="showindex.cfm">political situation</a></b></font></td>
<td><b>76</b></td>
</tr>
<tr>
<td><p title=" This is the political stability data;Score: 0.01;Sene:">
<a href="showdatatable.cfm">political stability denge</a></p></td>
<td> 7</td>
</tr>
<tr>
<td><p title="This index combines policies.;Score: -0.34;Sene:">
<a href="showdatatable.cfm">local government support</a></p></td>
<td> 8</td>
</tr>
<tr>
<td><p title="This combines legal situation data;Score: 3.59;Sene:">
<a href="showdatatable.cfm">legal situation</a></p></td>
<td > 9</td>
</tr>
我制作了一系列外部“td”标签。
我感兴趣的是:对于每个表行提取
1-如果有一个“p”标签,它就是标题属性
2-“a”的内部文本
3- 最后一个“td”标签的内部文本。并使它们成为元组, 喜欢
(" This is the political stability data;Score: 0.01;Sene:", "companies", "71");
我首先将每两个“td”标签设为一个元组(我的方法可能非常粗略,抱歉),然后提取我感兴趣的数据。 这是我的代码
tdSeq:seq<HtmlNode>
tdSeq
|>Seq.pairwise
|>Seq.mapi(fun int item -> (int, item))
|>Seq.filter(fun (no, _) -> no%2 = 0)
|>List.ofSeq
|>List.map(fun (no, item ) -> item)
|>List.map(fun (a, b) ->
let data = a.InnerText.Trim()
let value= b.InnerText.Trim()
let title=
let p= a.SelectSingleNode("//p" )
if p.Attributes.["title"] <> null then
p.Attributes.["title"].Value
else
""
(title, data, value))
我的问题是每个元组只返回第一个“p”标签的标题。 有什么提示吗?
【问题讨论】:
标签: html f# html-agility-pack