Java web crawler--download page picture

Source: Internet
Author: User

 

Package COM.HTTP5;

Import Java.io.InputStream;
Import Org.apache.commons.io.IOUtils;
Import org.apache.http.HttpEntity;
Import Org.apache.http.HttpResponse;
Import org.apache.http.client.HttpClient;
Import Org.apache.http.client.methods.HttpGet;

Import Org.apache.http.util.EntityUtils;
		public class Imageshow {public static String getimg (httpclient http,string url) {try{httpget get=new httpget (URL);
		HttpResponse Hr=http.execute (GET);
			Httpentity he=hr.getentity ();//鍝 堝 搱 if (he!=null) {String charset=entityutils.getcontentcharset (HE);
			InputStream is=he.getcontent ();
	Return ioutils.tostring (Is,charset);
	}}catch (Exception e) {e.printstacktrace ();
	
return null;
	public static byte[] getimage (httpclient http,string url) {try{httpget hg=new httpget (URL);
	HttpResponse Hr=http.execute (Hg);
	Httpentity he=hr.getentity ();
		if (he!=null) {InputStream is=he.getcontent ();
	Return Ioutils.tobytearray (IS);
	} catch (Exception e) {e.printstacktrace (); } return null;
 }
}

Package COM.HTTP5;

Import Java.io.FileOutputStream;
Import Org.apache.commons.io.FilenameUtils;
Import Org.apache.commons.io.IOUtils;
Import org.apache.http.client.HttpClient;
Import org.apache.http.impl.client.DefaultHttpClient;
Import Org.htmlparser.Parser;
Import Org.htmlparser.filters.NodeClassFilter;
Import Org.htmlparser.tags.ImageTag;



Import org.htmlparser.util.NodeList; public class Imagedownload {/** * @param args */public static void main (string[] args) {httpclient http=new De
		Faulthttpclient ();
		try{String html=imageshow.getimg (http, "http://www.baidu.com/"); Parser p=new Parser ();//Create parser p.setinputhtml (HTML);//Parse HTML nodelist nl=p.parse (new Nodeclassfilter (Imagetag.class))
	;//Select Image label for (int i=0;i<nl.size (); i++) {Imagetag image= (imagetag) nl.elementat (i);
	String Imageurl=image.getimageurl ()//Get Picture src attribute value string url= "http://www.baidu.com" +IMAGEURL;
	System.out.println (URL); String jpg=filenameutils.getname (URL);//Picture name byte[] Im=imageshoW.getimage (Http,url);
	Ioutils.write (IM, new FileOutputStream ("e:/temp/" +jpg));
	}}catch (Exception e) {e.printstacktrace ();
 }

	}

}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.