In real life we often see the information we need when we browse the Web, but the information is too large to be saved individually.
Next, let's take the email address as an example, crawl the email address in the Web page using the Java language
The following ideas are implemented:
1. Use Java.net.URL object to bind the address of a webpage on the network
2. Obtain a URLConnection object through the OpenConnection () method of the Java.net.URL object
3. The input stream object of the network file is obtained through the getInputStream () method of the URLConnection object InputStream
4. Iterate through each row of data in the stream, and compile the regular expression with the pattern object with each line of characters to get an email address
Next, the words don't say much, directly on the code:
ImportJava.io.BufferedReader; ImportJava.io.InputStreamReader; ImportJava.net.URL; Importjava.net.URLConnection; ImportJava.util.regex.Matcher; ImportJava.util.regex.Pattern; Public classTest { Public Static voidMain (string[] args)throwsException {//Set destination URL
URL URL=NewURL ("Destination URL"); //Open ConnectionURLConnection conn =url.openconnection (); //Setting the connection network timeout timeConn.setconnecttimeout (1000 * 10); //reads a file from a specified network addressBufferedReader BUFR =NewBufferedReader (NewInputStreamReader (Conn.getinputstream ()));
Build an empty string in memory to prepare to get the read mailbox string Line=null;
// set a regular expression matching email;
String Reg= "[A-za-z0-9_-][email protected]\\w+\\. [A-z]+ (\\.[ a-z]+)? "; Pattern p =Pattern.compile (REG);
Loop output read e-mail address while(line = Bufr.readline ())! =NULL) {Matcher m=P.matcher (line); while(M.find ()) {System.out.println (M.group ()); } } } }
My level is limited, can not read the above code in detail, but also hope to forgive
How to crawl an e-mail address in a Web page using the Java language