---------------------- ASP.Net+Unity開發、.Net培訓、期待與您交流! ----------------------
在看完了畢老師的基礎視頻後自己寫的爬號器:
理想中的爬號器:
可以掃描任任何文件,自動掃描關鍵字網頁,獲取所需數據
對號碼,比如QQ號碼實現郵件、加好友、空間留言等騰訊應用的各種輔助,自娛自樂功能
實現的爬號器:
獲取指定文件中符合規則數據並存儲到文件中
package Gane;
public class Tool {
public static void main(String args[]) throws IOException{
new MyFrame();
}
}
package Gane;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
//圖形化界面
class MyFrame{
private Frame f ;
private MenuBar mb;
private Menu m1, m2;
private MenuItem mi;
private TextField tf1, tf2;
private TextArea ta;
private Button bt1, bt2, bt3, bt4;
private Dialog d;
private FileDialog fd1, fd2;
MyFrame(){
init();
}
public void init(){
f= new Frame("TC爬號器");
f.setBounds(1100, 400, 600, 400);
f.setLayout(new FlowLayout());
mb = new MenuBar();
m1 = new Menu("菜單");
m2 = new Menu("其他");
mi = new MenuItem("退出");
mb.add(m1);
mb.add(m2);
m1.add(mi);
tf1 = new TextField(20);
tf2 = new TextField(20);
ta = new TextArea();
bt1 = new Button("選擇文件");
bt2 = new Button("獲取號碼");
bt3 = new Button("生成郵件地址");
bt4 = new Button("確定");
ta = new TextArea("使用說明....", 15, 60);
d = new Dialog(f,"提示", true);
d.setLayout(new FlowLayout());
d.setBounds(1300, 560, 50, 90);
d.add(bt4);
fd1 = new FileDialog(f, "選擇掃描文件", FileDialog.LOAD);
fd2 = new FileDialog(f, "保存生成文件", FileDialog.SAVE);
f.add(tf1);
f.add(bt1);
// f.add(tf2);
f.add(bt2);
f.add(bt3);
f.add(ta);
f.setMenuBar(mb);
f.setVisible(true);
MyEvent();
}
public void MyEvent(){
wC(f);
wC(d);
mi.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
d.setVisible(true);
}
});
bt1.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){ //mouseEntered
fd1.setVisible(true);
String file = fd1.getFile();
String path1 = fd1.getDirectory() + file;
String path = path1.replace("\\", "\\\\");
// System.out.println(path);
tf1.setText(path);
}
});
bt2.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){ //mouseEntered
String path = tf1.getText();
try {
//調用方法獲取號碼,並將反饋信息展示到ta中
ta.setText(new Number().getNumber(path));
} catch (IOException e1) {
ta.setText("讀取文件失敗");
new RuntimeException();
}
}
});
bt3.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){ //mouseEntered
//調用方法生成郵件地址,並將反饋信息展示到ta中
String path = tf1.getText();
try {
//調用方法獲取號碼,並將反饋信息展示到ta中
ta.setText(new Number().getMail(path));
} catch (IOException e1) {
ta.setText("讀取文件失敗");
new RuntimeException();
}
}
});
bt4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
}
public void wC(Frame f){
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void wC(Dialog d){
d.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
package Gane;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Number{
private String aim ="C:\\Users\\Administrator\\Desktop\\";
private FileWriter fw;
/*獲取號碼*/
public String getNumber(String path) throws IOException{
TreeSet ts = getTS(path);
//將數據寫到文件中
aim = aim + "QQNumber.txt";
fw = new FileWriter(aim);
fw.write("共掃到號碼:"+ts.size()+" 個\r\n");
Iterator<Long> it = ts.iterator();
while(it.hasNext()){
Long l = it.next();
fw.write(l + "\r\n");
fw.flush();
}
fw.close();
return "共掃到號碼:"+ts.size()+" 個\r\n";
}
/*生成郵件地址*/
public String getMail(String path) throws IOException{
TreeSet ts = getTS(path);
//將數據寫到文件中
aim = aim +"MainNumber.txt";
fw = new FileWriter(aim);
fw.write("共生成郵件地址:"+ts.size()+" 個\r\n");
Iterator<Long> it = ts.iterator();
while(it.hasNext()){
Long l = it.next();
fw.write(l + "@qq.com\r\n");
fw.flush();
}
fw.close();
return "共生成郵件地址:"+ts.size()+" 個\r\n";
}
private TreeSet getTS(String path) throws IOException{
FileReader fr = new FileReader(path);
TreeSet<Long> ts = new TreeSet<Long>();
char[] buf = new char[1024];
int ch = 0;
while((ch = fr.read(buf)) != -1)
{
String str = new String(buf, 0, ch);
String reg = "[1-9][0-9]{7,10}";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
while(m.find())
{
String s = m.group();
long l = Long.parseLong(s);
ts.add(l);
}
}
fr.close();
return ts;
}
}
---------------------- ASP.Net+Unity開發、.Net培訓、期待與您交流! ----------------------