package com.xqx.htmlunit;


import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

public class HtmlunitTesPic {

	private static final String BASE_URL = "http://pic.sogou.com";
	private static final String KEY_WORD = "张学友";
	
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52);
		webClient.getOptions().setCssEnabled(Boolean.TRUE);
		webClient.getOptions().setJavaScriptEnabled(Boolean.TRUE);
		webClient.getOptions().setThrowExceptionOnFailingStatusCode(Boolean.FALSE);  
        webClient.setAjaxController(new NicelyResynchronizingAjaxController()); 
        webClient.waitForBackgroundJavaScript(600*1000);    
//        new SimpleConectionListener(webClient);
		try {
			HtmlPage page = webClient.getPage(BASE_URL);
			//System.out.println(page.asXml());
			
			HtmlForm searchForm = page.getFormByName("searchForm");
			HtmlTextInput keyWordInput = searchForm.getInputByName("query");
			keyWordInput.setValueAttribute(KEY_WORD);
			HtmlSubmitInput submitInput = (HtmlSubmitInput) searchForm.getByXPath("//div/input[@type=\"submit\"]").get(0);
			HtmlPage returnPage = submitInput.click();
			webClient.waitForBackgroundJavaScript(6*1000);    
			//System.out.println(returnPage.asXml());
			
			
			HtmlAnchor imgDivA = (HtmlAnchor) returnPage.getByXPath("//div[@class='seachOptionHint']/ul/li/a[@class='picContainer']").get(0);  
			String imgQueryUrl = imgDivA.getAttribute("href");
			String queryUrl = imgQueryUrl.substring(0, imgQueryUrl.length()-1);
			for (int i = 1; i < 10; i++) {
				HtmlPage imgPage = webClient.getPage(BASE_URL+queryUrl+i);
				HtmlImage image = (HtmlImage)imgPage.getByXPath("//div[@id=\"thumbBox\"]/a[@id=\"imageBox\"]/img").get(0);
				String str = image.getAttribute("src");
				if(str.contains(".jpg")){
					int end = str.indexOf(".jpg")+4;
					str = str.substring(0, end);
					download(URLDecoder.decode(str),"E:\\htmlutil\\pic\\");
				}
				System.out.println(i);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

	private static void download(String url, String path) {
        File file= null;  
        File dirFile=null;  
        FileOutputStream fos=null;  
        HttpURLConnection httpCon = null;  
        URLConnection  con = null;  
        URL urlObj=null;  
        InputStream in =null;  
        byte[] size = new byte[1024];  
        int num=0;  
        try {  
            String downloadName = url.substring(url.lastIndexOf("/")+1);  
            dirFile = new File(path);  
            if(!dirFile.exists()){  
                if(dirFile.mkdir()){  
                    if(path.length()>0){  
                        System.out.println("creat document file \""+path.substring(0,path.length()-1)+"\" success...\n");  
                    }  
                }  
            }else{  
            file = new File(path+downloadName);  
            fos = new FileOutputStream(file);  
            if(url.startsWith("http")){  
                urlObj = new URL(url);  
                con = urlObj.openConnection();  
                httpCon =(HttpURLConnection) con;  
                in = httpCon.getInputStream();  
                while((num=in.read(size)) != -1){  
                    for(int i=0;i<num;i++)   
                       fos.write(size[i]);  
                }  
            }  
            }  
              
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally{  
            try {  
                fos.close();  
                in.close();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
	}
	
}
最近下载更多
a837535114  LV10 2020年5月19日
刘孟飞  LV19 2020年3月29日
1715656022  LV1 2020年3月19日
x201720030132  LV3 2019年12月25日
newhaijun  LV15 2019年11月14日
xuguangyue120  LV9 2019年8月8日
psyche  LV1 2019年4月26日
zhangguobin  LV14 2019年4月24日
hello王源  LV12 2019年4月18日
蛇头凌志  LV8 2019年2月22日
最近浏览更多
夜上清元  LV8 2022年5月4日
2206371875  LV7 2022年4月25日
crosa_Don  LV18 2022年4月1日
daima119977 2022年3月15日
暂无贡献等级
18784679855  LV8 2022年3月12日
心印语  LV9 2022年3月3日
故渊  LV1 2021年12月24日
Aoifee  LV4 2021年11月11日
君亦逍遥  LV2 2021年6月4日
别碰我被窝  LV9 2021年4月8日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友