【问题标题】:extracting text from specific tags in html using Mathematica使用 Mathematica 从 html 中的特定标签中提取文本
【发布时间】:2015-07-13 21:34:07
【问题描述】:

对于这样结构的html页面:

          <tr class="">
            <td class="number">1</td>
            <td class="name"><a href="..." >Jack Green</a></td>
            <td class="score-cell ">
              <span class="display">98
                <span class="tooltip column1"></span>
              </span>
            </td>
            <td class="score-cell ">
              ...
            </td>
          ...
          <tr class="">
            <td class="number">2</td>
            <td class="name"><a href="..." target="_top">Nicole Smith</a></td>
            <td class="score-cell ">
             ...
            </td>

如何仅从名称标签中提取文本以得到列表{Jack Green, Nicole Smith}?我希望一些优雅的方法。

【问题讨论】:

    标签: html xml parsing tags wolfram-mathematica


    【解决方案1】:
    input =
      "          <tr class=\"\">
                  <td class=\"number\">1</td>
                  <td class=\"name\"><a href=\"...\" >Jack Green</a></td>
                  <td class=\"score-cell \">
                    <span class=\"display\">98
                      <span class=\"tooltip column1\"></span>
                    </span>
                  </td>
                  <td class=\"score-cell \">
                    ...
                  </td>
                ...
                <tr class=\"\">
                  <td class=\"number\">2</td>
                  <td class=\"name\"><a href=\"...\" target=\"_top\">Nicole Smith</a></td>
                  <td class=\"score-cell \">
                   ...
                  </td>";
    
    (* Eliminate unnecessary whitespace and add a start character *)
    html = StringJoin["X", StringReplace[StringTrim[input],
       {"\n" ~~ " " .. -> "", ">" ~~ " " .. ~~ "<" -> "><"}]];
    
    (* Find the tags and positions of tags containing 'name' *)
    tags = StringCases[html, "<" ~~ Except[">"] .. ~~ ">"];
    nametagpositions = Position[StringMatchQ[ToLowerCase /@ tags, "*name*"], True];
    
    (* Split on the tags and extract on the name tag positions *)
    splits = StringSplit[html, "<" ~~ Except[">"] .. ~~ ">"];
    Extract[splits, nametagpositions + 2]
    

    {杰克·格林,妮可·史密斯}

    注意

    需要起始字符来保证正确拆分。正如您在下面的演示中所见,a 字符之间的初始拆分在有要报告的子字符串之前不计算在内。使用起始字符可以可靠地使用所需项目的位置。

    html = "aa1aaa2aa";
    splits = StringSplit[html, "a"]
    

    {1, , ,2}

    html = "aaaaaaa1aaa2aaaaaaa";
    splits = StringSplit[html, "a"]
    

    {1, , ,2}

    html = "0aaaaaaa1aaa2aaaaaaa";
    splits = StringSplit[html, "a"]
    

    {0, , , , , , ,1, , ,2}

    【讨论】:

    • 这太棒了,想知道是否最好封装成一个函数。让我继续在一些网页源 html 上进行测试。感谢您的字符串操作技巧。
    • 所以我继续测试它,其中之一:input=Import["http://games.crossfit.com/scores/leaderboard.php?stage=5&amp;sort=0&amp;division=1&amp;region=0&amp;regional=6&amp;numberperpage=60&amp;userid=0&amp;competition=0&amp;frontpage=0&amp;expanded=1&amp;year=15&amp;full=1&amp;showtoggles=0&amp;hidedropdowns=1&amp;showathleteac=1&amp;athletename=&amp;scaled=0","Source"]; 并且输出似乎无法正确生成输出。有问题的标签是&lt;td class="name"&gt;
    • 我已经修复了代码。它现在可以在您的 CrossFit 页面上使用。有 2 个变化:添加起始字符并增加 Extract 位置(在最后一行)。
    猜你喜欢
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    相关资源
    最近更新 更多