简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM