无心制作

时间:2019-11-18
本文章向大家介绍无心制作,主要包括无心制作使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Main {

    public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
        final WebClient webClient=new WebClient(BrowserVersion.CHROME);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        final HtmlPage page=webClient.getPage("http:");
        DomNodeList<DomElement> div=page.getElementsByTagName("img");
        
        div.forEach(item->{
            String line=item.getAttribute("src").toString();
            if((line.startsWith("http")||line.startsWith("https"))&&(Pattern.compile("\\d+.jpg").matcher(line)).find())
            {
                try {
                    URL url=new URL(item.getAttribute("src"));
                    BufferedInputStream in=new BufferedInputStream(url.openStream());
                    FileOutputStream file=new FileOutputStream(new File(line.substring(line.lastIndexOf("/")+1)));
                    int bit=0;
                    while((bit=in.read())!=-1)
                    {
                        file.write(bit);
                    }
                    
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println(item.getAttribute("src"));
            }
        });
        System.out.println(div.size());
        webClient.close();
    }

}

原文地址:https://www.cnblogs.com/z2529827226/p/11882600.html