开发者

Extract link of background, jsoup

开发者 https://www.devze.com 2023-03-10 01:16 出处:网络
I have a problem with extract link from HTML of the following nature, using jsoup. <div class=\"post_video\" style=\"background-image:url(http://img.youtube.com/vi/JFf3uazyXco/2开发者_StackOverflo

I have a problem with extract link from HTML of the following nature, using jsoup.

<div class="post_video" style="background-image:url(http://img.youtube.com/vi/JFf3uazyXco/2开发者_StackOverflow社区.jpg);">


This is how I'd do it.

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(")") ) );
        }
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消