1、实现语言:java;
2、实现原理:
a)连接网络--采用java连接网络的方法。URLConnection.
b)读取网页信息。利用正则表达式查找网页中ip地址信息。
c)保存查找的地址信息到文件web.txt里。(以追加的方式)
d)Web.txt:
IP---------------------->连接时系统时间。
实现代码共享如下。
包含两个文件:main_.java,webCatch.java
运行之后会生成web.txt。
Main_.java
public class main_ {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
webCatch wc=new webCatch();
String regex="";
wc.setRegex(regex);
wc.setUrlStr("http://www.ip138.com/ip2city.asp");
wc.show();
}
}
webCatch.java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class webCatch {
String regex = "[0-9]{1,3}\\\\.[0-9]{1,3}\\\\.[0-9]{1,3}\\\\.[0-9]{1,3}";
Matcher mt = null;
Pattern pt = Pattern.compile(regex);
URL url = null;
// String urlStr = "http://www.ip138.com/ip2city.asp";
String urlStr = "http://www.baidu.com";
public String getDate(){
Date no=new Date();
Calendar cal=Calendar.getInstance();
DateFormat dmt=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);
return dmt.format(no);
}
public String getRegex() {
return regex;
}
public void setRegex(String regex) {
this.regex = regex;
}
public String getUrlStr() {
return urlStr;
}
public void setUrlStr(String urlStr) {
this.urlStr = urlStr;
}
public void show(){
try {
this.url = new URL(this.urlStr);
URLConnection conn = this.url.openConnection();
conn.setDoOutput(true);
InputStream in = this.url.openStream();
String txt = this.getWeb(in);
in.close();
// System.out.print(txt);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
// System.out.print("网络连接失败");
}
}
/**
* @param args
*/
private String getWeb(InputStream in) throws IOException {
// TODO Auto-generated method stub
StringBuffer s = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
PrintWriter pr = null;
String line = null;
StringBuffer cc = new StringBuffer();
FileOutputStream fo = null;
try {
fo = new FileOutputStream("web.txt",true);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OutputStreamWriter wr = new OutputStreamWriter(fo);
pr = new PrintWriter(wr);
while ((line = br.readLine()) != null) {
String sss = line;
if (sss.length() > 0) {
s.append(sss);
/* pr.print(sss);
pr.flush();*/
}
}
mt = pt.matcher(s.toString());
while (mt.find()) {
cc.append(mt.group());
cc.append("---->"+this.getDate()+"\\r\\n");
}
// System.out.print(cc.toString());
pr.print(cc);
pr.flush();
line = null;
in.close();
pr.close();
wr.close();
fo.close();
br.close();
return s.toString();
}
}
此程序只要稍加修改,就可以提取网络中任何信息。
修改主程序的main方法即可。下载本文