Jsoup - 改进从html页面提取图像

时间:2014-01-19 09:08:41

标签: java html image jsoup

我正在使用以下代码行从网络上获取图片:

for(int i=0; i<links.size(); i++){ 
        try{
            doc=Jsoup.connect(links.get(i)).userAgent("Mozilla").ignoreHttpErrors(true).timeout(0).get();
            Elements links=doc.getElementsByTag("img");
            imageLink=links.get(3).toString();
            String[] bits=imageLink.split("\"");
            imageLink=bits[1];
            System.out.println(imageLink);
            url=new URL(imageLink);
            image=ImageIO.read(url);
            images.add(image);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
}

此代码效果很好,但速度非常慢。我得到每秒一个图像,我至少需要一半的时间。我能做些什么来改善它吗?

1 个答案:

答案 0 :(得分:1)

你可以替换它:

imageLink=links.get(3).toString();
String[] bits=imageLink.split("\"");
imageLink=bits[1];

有了这个:

imageLink = links.get(3).attr("src");

在此处阅读有关提取属性的详情:http://jsoup.org/cookbook/extracting-data/attributes-text-html

相关问题