---------------------- 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()+" 个 "); Iterator<Long> it = ts.iterator(); while(it.hasNext()){ Long l = it.next(); fw.write(l + " "); fw.flush(); } fw.close(); return "共扫到号码:"+ts.size()+" 个 "; } /*生成邮件地址*/ public String getMail(String path) throws IOException{ TreeSet ts = getTS(path); //将数据写到文件中 aim = aim +"MainNumber.txt"; fw = new FileWriter(aim); fw.write("共生成邮件地址:"+ts.size()+" 个 "); Iterator<Long> it = ts.iterator(); while(it.hasNext()){ Long l = it.next(); fw.write(l + "@qq.com "); fw.flush(); } fw.close(); return "共生成邮件地址:"+ts.size()+" 个 "; } 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培训、期待与您交流! ----------------------