The essence of the crawler is to open the Web page source code for matching lookup, and then get the results found.
/*
* Get
* Encapsulates the regular rule object.
* Pattern p = pattern.compile ("A*b");
*//is associated with the Matcher method string of the regular object. Gets the Matcher object to be manipulated against the string.
* Matcher m = p.matcher ("Aaaaab");
*//The string is manipulated by means of the Matcher object.
* Boolean B = m.matches ();
*/
Package Com.js.ai.modules.pointwall.testxfz;import Java.io.bufferedreader;import Java.io.filereader;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.net.malformedurlexception;import Java.net.URL; Import Java.util.arraylist;import java.util.list;import java.util.regex.matcher;import Java.util.regex.Pattern; public class Spider {public static list<string> Getmailsbyweb () throws Ioexception{//1, reads the source file. URL url=new url ("http://www.cnblogs.com/Renyi-Fan/p/6896901.html"); BufferedReader bufr=new BufferedReader (New InputStreamReader (Url.openstream ()));//2, the matching of rules for the data being read. Gets the data that conforms to the rule from. String Mail_regex = "\\[email protected]\\w+ (\\.\\w+) +"; List<string> list=new arraylist<string> (); Pattern p = pattern.compile (Mail_regex); String line = Null;while ((Line=bufr.readline ())!=null) {Matcher m = P.matcher (line) and while (M.find ()) {//3, Stores the data that conforms to the rule in the collection. List.add (M.group ());}} return list;} public static list<string> Getmails () throws Ioexception{//1, reading the source file. BufferedReader Bufr=new BufferedReader (New FileReader ("c:\\mail.html"));//2, matching rules for the data being read. Gets the data that conforms to the rule from. String Mail_regex = "\\[email protected]\\w+ (\\.\\w+) +"; list<string> list = new arraylist<string> (); Pattern p = pattern.compile (Mail_regex); String line = null; while ((Line=bufr.readline ())!=null) {Matcher m = p.matcher (line); while (M.find ()) {//3, the data that conforms to the rule is stored in the collection. List.add (M.group ()); }}return list;} public static void Main (string[] args) throws IOException {//List<string> List = Getmails ();//For (String Mail: List) {//System.out.println (mail);//}list<string> List=getmailsbyweb (); for (String mail:list) { System.out.println (mail);}}}
Java Crawler Simple Example