"Beginner" Java crawler and grab pictures to save

Source: Internet
Author: User

This is my reference to some information on the Web to write the first Java crawler program

Originally want to get fried egg net boring picture, but the network return code is always 503, so changed the site


* * * Web crawler FETCH DATA * */public class Jiandan {public static string GetUrl (String inurl) {StringBuilder sb = new String
		Builder ();
			try {URL url =new url (inurl);
			
			BufferedReader Reader =new BufferedReader (New InputStreamReader (Url.openstream ()));
			String temp= "";
				while ((Temp=reader.readline ())!=null) {//system.out.println (temp);
			Sb.append (temp);
		The Catch Block E.printstacktrace () is automatically generated by catch (Malformedurlexception e) {//TODO);
		catch (IOException e) {//TODO automatically generated catch block E.printstacktrace ();
	return sb.tostring (); public static list<string> getmatcher (String str,string URL) {list<string> result = new Arraylist<stri
		Ng> ();
		Pattern p =pattern.compile (URL);//Get web address Matcher m =p.matcher (str);
			while (M.find ()) {//system.out.println (M.group (1));
		Result.add (M.group (1));
	return result;
		public static void Main (string args[]) {string Str=geturl ("http://www.163.com"); list<string> ouput =getmaTcher (str, "src=\") ([\\w\\s./:]+?)
		
		\"");
			for (String temp:ouput) {//system.out.println (ouput.get (0));
		SYSTEM.OUT.PRINTLN (temp);
		 } String aurl=ouput.get (0);
		Constructs URL URL URLs;
			 try {url = new URL (aurl);
			 Open URL connection urlconnection con = (urlconnection) url.openconnection ();
			Get the URL of the input stream InputStream input = Con.getinputstream ();
			Set data buffering byte[] bs = new byte[1024 * 2];
			The length of data read int len;
			The output file stream saves the picture to the local outputstream OS = new FileOutputStream ("A.png");
			while (len = Input.read (BS))!=-1) {os.write (BS, 0, Len);
			} os.close ();
		Input.close ();
		catch (Malformedurlexception e) {//TODO automatically generated catch block E.printstacktrace ();
		catch (IOException e) {//TODO automatically generated catch block 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.