Implementation ideas:
1. Use a java.net. URL object to bind a webpage address on the network
2. Obtain an HttpConnection object through the openConnection () method of the java.net. URL object.
3. Use the getInputStream () method of the HttpConnection object to obtain the input stream object InputStream of the network file.
4. read each row of data in the stream cyclically, and use the regular expression compiled by the Pattern object to partition each row of characters to obtain the email address.
[Java]
Package regex;
Import java. io. BufferedReader;
Import java. io. InputStreamReader;
Import java.net. URL;
Import java.net. URLConnection;
Import java. util. regex. Matcher;
Import java. util. regex. Pattern;
/**
* Web crawlers capture the email addresses on the webpage
*/
Public class WebCrawlersDemo {
Public static void main (String [] args) throws Exception {
URL url = new URL ("http://www.tianya.cn/publicforum/content/english/1/129176.shtml ");
// Open the connection
URLConnection conn = url. openConnection ();
// Set the network connection timeout.
Conn. setConnectTimeout (1000*10 );
// Read files from the specified network address
BufferedReader bufr = new BufferedReader (new InputStreamReader (conn. getInputStream ()));
String line = null;
String regex = "[a-zA-Z0-9 _-] + @ \ w + \. [a-z] + (\. [a-z] + )? "; // Match the regular expression of the email
Pattern p = Pattern. compile (regex );
While (line = bufr. readLine ())! = Null ){
Matcher m = p. matcher (line );
While (m. find ()){
System. out. println (m. group (); <span style = "white-space: pre"> </span> // obtain the matching email
}
}
}
}
Result:
Author: xyang81