Java Crawler Simple Example

Source: Internet
Author: User

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

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.