A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© sniper170fly 中级黑马   /  2015-10-6 19:11  /  586 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package cn.com.netexe;

  2. /*
  3. 自定义浏览器,显示网页信息
  4. */
  5. import java.io.*;//导包
  6. import java.awt.*;
  7. import java.awt.event.*;
  8. import java.net.*;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;

  11. class MyGui
  12. {
  13.         private Frame f;//定义为成员变量,方便调用
  14.         private Button but;
  15.         private TextField tf;
  16.         private TextArea ta;

  17.         private Dialog d;
  18.         private Label lab;
  19.         private Button okBut;

  20.         MyGui()//构造函数
  21.         {
  22.                 init();
  23.         }

  24.         public void init()
  25.         {
  26.                 f=new Frame("my window");//创建窗体,名为my window
  27.                 f.setBounds(300,100,600,500);//左,上,长,宽
  28.                 f.setLayout(new FlowLayout());//流式布局管理器

  29.                 tf=new TextField(60);//文本框长度

  30.                 but=new Button("转到");//创建按钮,名为转到

  31.                 ta=new TextArea(25,70);

  32.                 d=new Dialog(f,"提示信息—self",true);//在窗体中关联对话框
  33.                 d.setBounds(400,200,240,150);
  34.                 d.setLayout(new FlowLayout());
  35.                 lab=new Label();//创建标签
  36.                 okBut=new Button("确定");

  37.                 d.add(lab);//添加组件到对话框
  38.                 d.add(okBut);

  39.                 f.add(tf);//添加组件到窗体
  40.                 f.add(but);
  41.                 f.add(ta);

  42.                 myEvent();
  43.                 f.setVisible(true);//窗体可见
  44.         }
  45.         private void myEvent()
  46.         {
  47.                 okBut.addActionListener(new ActionListener()//动作监听器
  48.                 {
  49.                         public void actionPerformed(ActionEvent e)
  50.                         {
  51.                                 d.setVisible(false);
  52.                         }
  53.                 });
  54.                 d.addWindowListener(new WindowAdapter()//窗体监听器
  55.                 {
  56.                         public void windowClosing(WindowEvent e)
  57.                         {
  58.                                 d.setVisible(false);
  59.                         }
  60.                 });
  61.                 tf.addKeyListener(new KeyAdapter()//键盘监听器
  62.                 {
  63.                         public void keyPressed(KeyEvent e)
  64.                         {
  65.                                 try
  66.                                 {
  67.                                         if(e.getKeyCode()==KeyEvent.VK_ENTER)
  68.                                                 showDir();
  69.                                 }
  70.                                 catch (Exception ex)
  71.                                 {
  72.                                 }
  73.                         }
  74.                 });

  75.                 but.addActionListener(new ActionListener()//动作监听器
  76.                 {
  77.                         public void actionPerformed(ActionEvent e)
  78.                         {
  79.                                 try
  80.                                 {
  81.                                         showDir();
  82.                                 }
  83.                                 catch (Exception ee)
  84.                                 {
  85.                                 }
  86.                         }
  87.                 });

  88.                 f.addWindowListener(new WindowAdapter()//窗体监听器
  89.                 {
  90.                         public void windowClosing(WindowEvent e)
  91.                         {
  92.                                 System.exit(0);
  93.                         }
  94.                 });
  95.                
  96.                 tf.addActionListener(new ActionListener() {
  97.                        
  98.                         public void actionPerformed(ActionEvent e) {
  99.                         }
  100.                 });
  101.                
  102.                 tf.addKeyListener(new KeyAdapter()
  103.                 {
  104.                         public void keyPressed(KeyEvent ek)
  105.                         {
  106.                                 if(ek.getKeyCode()==KeyEvent.VK_ENTER)
  107.                                 {
  108.                                         try {
  109.                                                 showDir();
  110.                                         } catch (Exception e) {
  111.                                                 e.printStackTrace();
  112.                                         }
  113.                                 }
  114.                         }
  115.                 });

  116.         }

  117.         private void showDir()throws Exception
  118.         {
  119.                 ta.setText("");//清空文本框内容
  120.                 //获取输入路径
  121.                 String urlPath=tf.getText();//http://192.168.1.254:8080/maweb/demo.html

  122.                 URL url=new URL(urlPath);//封装地址对象

  123.                 URLConnection conn=url.openConnection();//连接网页服务器,openConnection里面封装了socket服务
  124.                 BufferedReader bufr = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  125.                 String str = null;
  126.                
  127.                 //定义正则表达式
  128.                 String regex = "\\w+@\\w+(\\.\\w+)+";
  129.                 Pattern p = Pattern.compile(regex);
  130.                
  131.                 while((str=bufr.readLine())!=null)
  132.                 {
  133.                         Matcher m = p.matcher(str);
  134.                         while(m.find())
  135.                         {
  136.                                 System.out.println(m.group());
  137. //                                ta.setText(m.group());
  138.                                 ta.append(m.group()+"\r\n");
  139.                         }
  140.                 }
  141.                
  142.         }

  143.         public static void main(String[] args)
  144.         {
  145.                 new MyGui();
  146.         }
  147.        
  148.        
  149. }
复制代码

可以通过在地址栏输入网站地址,将该网站的qq信息爬取出来。

QQ截图20151006191114.png (74.4 KB, 下载次数: 0)

爬取李毅吧的qq邮箱

爬取李毅吧的qq邮箱

8 个回复

倒序浏览
这个爬虫是,抓取什么数据的
回复 使用道具 举报
邱洋 发表于 2015-10-6 19:25
这个爬虫是,抓取什么数据的

抓qq号码啊
回复 使用道具 举报
能爬所有的网页吗
回复 使用道具 举报
邱洋 发表于 2015-10-6 20:10
能爬所有的网页吗

有qq号的就能爬,你可以去贴吧里找一个有qq号码的帖子试试
回复 使用道具 举报
哦!改天看看
回复 使用道具 举报
再加两个TextField:  一个填正则,一个编码方式。 可以打包自用了
回复 使用道具 举报
iceknc 中级黑马 2015-10-6 21:23:45
8#
写得真666
回复 使用道具 举报
好东西,学习了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马