【问题标题】:Use Enlive to match a specific TD tag in a group of TD tags使用 Enlive 匹配一组 TD 标签中的特定 TD 标签
【发布时间】:2015-04-01 17:52:09
【问题描述】:

我刚刚开始使用 Elive 进行 HTML 屏幕抓取任务。如果我想要下表的第二个和第四个 TD 节点中的文本,我将如何指定选择器?我通读了教程,但没有找到任何关于如何指定 XPath 内容的示例:

html/body/table/tr/td[2]/td[4](假设从 1 开始的索引)

<html>
<body>
<table width="100%"  border="0" cellspacing="3" cellpadding="2">
  <tr>
    <td width="15%" class="labels">Part No</td>
    <td class="datafield">I2013-00007</td>
    <td class="labels"><div align="right">Parcel No</div></td>
    <td colspan="3" class="datafield">07-220-12-03-01-2-00-000</td>
  </tr>
</table>
</body>
</html>

我需要从这两个 TD 节点捕获文本值。

【问题讨论】:

  • 您的具体问题是什么,为什么要阅读文本..?你是要替换内容还是其他目的

标签: clojure enlive


【解决方案1】:

你可以像这样使用nth-of-type

user> (require '[net.cgrand.enlive-html :as html])
nil
user> (def test-html 
"<html><body><table width='100%'  border='0' cellspacing='3' cellpadding='2'><tr><td width='15%' class='labels'>Part No</td><td class='datafield'>I2013-00007</td><td class='labels'><div align='right'>Parcel No</div></td><td colspan='3' class='datafield'>07-220-12-03-01-2-00-000</td></tr></table></body></html>")
#'user/test-html
user> (:content (first (html/select (html/html-resource (java.io.StringReader. test-html)) [[:td (html/nth-of-type 2)]])))
("I2013-00007")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    • 2016-05-02
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多