【问题标题】:why returning the same html tag attribute every time?为什么每次都返回相同的 html 标签属性?
【发布时间】: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


    【解决方案1】:

    我不熟悉 F#,但这里是 C# 等价物:

    HtmlDocument doc = LoadMyDocument();
    
    foreach (HtmlNode tr in doc.DocumentNode.SelectNodes("tr"))
    {
        string title = null;
        HtmlNode titleNode = tr.SelectSingleNode(".//p");
        if (titleNode != null)
        {
            title = titleNode.GetAttributeValue("title", null);
        }
    
        string anchor = null;
        HtmlNode anchorNode = tr.SelectSingleNode(".//a");
        if (anchorNode != null)
        {
            anchor = anchorNode.InnerText;
        }
    
        string value = null;
        HtmlNode valueNode = tr.SelectSingleNode("td[last()]");
        if (valueNode != null)
        {
            value = valueNode.InnerText.Trim();
        }
    
        Console.WriteLine("title=" + title);
        Console.WriteLine("anchor=" + anchor);
        Console.WriteLine("value=" + value);
    }
    

    您遇到的主要问题是因为您使用从根开始的“//p”表达式,而不是从当前节点开始的“.//p”。

    【讨论】:

    • 谢谢,这真的很有帮助。我想我必须改变标题。
    猜你喜欢
    • 2019-01-06
    • 1970-01-01
    • 2023-02-25
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    相关资源
    最近更新 更多