提取背景链接,jsoup

jum*_*r0k 2 java background-image jsoup

我使用jsoup从以下性质的HTML中提取链接有问题.

<div class="post_video" style="background-image:url(http://img.youtube.com/vi/JFf3uazyXco/2.jpg);">
Run Code Online (Sandbox Code Playgroud)

Mik*_*wan 6

我就是这样做的.

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


public class JSoup {
    public static void main(String[] args) {
        String html = "<html><head></head><body><div class=\"post_video\" style=\"background-image:url(http://img.youtube.com/vi/JFf3uazyXco/2.jpg);\"></body></html>";

        Document doc = Jsoup.parse( html );
        Elements elements = doc.getElementsByClass("post_video");

        for( Element e : elements ) {
            String attr = e.attr("style");
            System.out.println( attr.substring( attr.indexOf("http://"), attr.indexOf(")") ) );
        }
    }

}
Run Code Online (Sandbox Code Playgroud)