搜索包含jsoup中特定文本的Image类

时间:2014-11-29 05:37:45

标签: java android parsing jsoup

如果我在div中有以下图像,我怎么能做一个jsoup查询只能返回那些包含文本“absmiddle”的文字的图像?

<div>
       <img class="absmiddle-blah foo-bar" src="google.com/image.png" \>
       <img class="absmiddle-blah2 foo-bar2" src="google.com/image.png" \>
       <img class="ablehe-bla2h foo-bar3" src="google.com/image.png" \>
       <img class="absmiddle-bl2ah foo-bar4" src="google.com/image.png" \>
       <img class="blah-bl2ah foo-bar4" src="google.com/image.png" \>
       <img class="absmiddle-bl2ah foo-bar4" src="google.com/image.png" \>
       <img class="sdfdle-bl2ah foo-bar4" src="google.com/image.png" \>
</div>

1 个答案:

答案 0 :(得分:1)

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class Main {

    public static void main(String[] args) throws Exception {
        String html =   "<div>"+
                               "<img class=\"absmiddle-blah foo-bar\" src=\"google.com/image.png\" \\>"+
                               "<img class=\"absmiddle-blah2 foo-bar2\" src=\"google.com/image.png\" \\>"+
                               "<img class=\"ablehe-bla2h foo-bar3\" src=\"google.com/image.png\" \\>"+
                               "<img class=\"absmiddle-bl2ah foo-bar4\" src=\"google.com/image.png\" \\>"+
                               "<img class=\"blah-bl2ah foo-bar4\" src=\"google.com/image.png\" \\>"+
                               "<img class=\"absmiddle-bl2ah foo-bar4\" src=\"google.com/image.png\" \\>"+
                               "<img class=\"sdfdle-bl2ah foo-bar4\" src=\"google.com/image.png\" \\>"+
                        "</div>";

        Document doc = Jsoup.parse(html);
        Elements els = doc.select("img[class*=absmiddle]");
        System.out.println(els);
    }
}

您可以查看here以获取有关Jsoup功能的更多示例