当前位置: 动力学知识库 > 问答 > 编程问答 >

java - How to get tag name for given word in Jsoup?

问题描述:

I have some html code like this:

<div class="post-text" itemprop="text"><a href="something.com">sometext for example</a></div>

I'm searching sometext word using jsoup and I want it's tag name. For above example it will be a href. Can anyone help me?

网友答案:

Try this CSS selector:

*:containsOwn(sometext)

DEMO

http://try.jsoup.org/~1FKtzLpHQFii4u8FFyUuh3GgdPI

SAMPLE CODE

String html = "<div class=\"post-text\" itemprop=\"text\"><a href=\"something.com\">sometext for example</a></div>";

Document doc = Jsoup.parse(html);
Elements elts = doc.select("*:containsOwn(sometext)");

for(Element e : elts) {
   System.out.println(e.outerHtml());
}

OUTPUT

<a href="something.com">sometext for example</a>

SEE ALSO

  • :matchesOwn(regex) - If you want to find element with more elaborate text.
  • Jsoup CSS selector - The complete reference on CSS selectors supported by Jsoup
分享给朋友:
您可能感兴趣的文章:
随机阅读: