【问题标题】:using logical or,not,and in jsoup selectors在 jsoup 选择器中使用逻辑或、非和和
【发布时间】:2013-03-16 15:00:59
【问题描述】:

使用这个

for (Element link : links) {
    String linkHref = link.attr("href and !#");       
    String linkText = link.text();       
}

我可以得到所有有"a href=.."的链接

不过也有一些

href="#"

我不需要在我的字符串中。 所以我需要做类似的事情

String linkHref = link.attr("href and !#")

即我不想将具有"#" 的链接保存为href。

这是可能的还是我必须使用正则表达式来代替?

请帮忙。

【问题讨论】:

  • 这看起来既不像 jQuery 也不像 CSS。请正确标记和标题您的问题,并阅读stackoverflow.com/editing-help
  • 您在询问一个特定的 API——一个特定的库。这可能没问题,但是您需要指出您要询问的 what 库。例如,您的Element 显然不是the org.w3c.dom.Element interface,因为该接口没有attr 方法。
  • 您可能只需要这样一个简单的检查:String linkHref = link.attr("href"); if (linkHref == "#") continue;
  • 哦,我真的很抱歉。我正在使用 jsoup 库。
  • @BoyLittUp 我的回答没有解决你的问题。只是问

标签: java html jsoup


【解决方案1】:

Jsoup选择器组合选择接受逗号

doc.select("[href], [src]");  // href **OR** src

对于 AND,只需将它们组合在一个 CSS 选择器中即可。检查这个answer

doc.select("a[href][:not(href='#')]");

【讨论】:

    【解决方案2】:

    阅读您的问题后,您似乎想要选择所有没有'#'作为href的锚标记。你可以使用:not Selector:

    Elements links = doc.select("a[href]"); // All anchor tags with href
    links = links.select(":not(href='#')"); // Filter out links which do have href=#
    

    【讨论】:

      猜你喜欢
      • 2011-01-24
      • 2017-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      • 2014-11-16
      • 2013-11-25
      • 2011-02-24
      相关资源
      最近更新 更多