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 (); }
}
}