How to crawl an e-mail address in a Web page using the Java language

Source: Internet
Author: User

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

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.