乱乱的代码段。。

 最近都在海峡人才网上看交流会信息:http://app.hxrc.com/services/rcjlh/jlhxx.aspx?id=1

但是它们的搜索似乎不能用?每次都要点开看企业的详细资料,好痛苦。。

今天又要招聘会了,偏偏半夜更新详细信息。

于是随意写了段乱乱的代码,没有注释,UI也随便乱写,可用就行。

界面长这样:输入人才交流会编号(在网页上点到链接的地方会看到rq=?)

 

Code:
  1. import java.awt.BorderLayout;   
  2. import java.awt.GridLayout;   
  3. import java.awt.event.ActionEvent;   
  4. import java.awt.event.ActionListener;   
  5. import java.io.BufferedReader;   
  6. import java.io.IOException;   
  7. import java.io.InputStreamReader;   
  8. import java.net.HttpURLConnection;   
  9. import java.net.MalformedURLException;   
  10. import java.net.URL;   
  11.   
  12. import javax.swing.JButton;   
  13. import javax.swing.JFrame;   
  14. import javax.swing.JLabel;   
  15. import javax.swing.JOptionPane;   
  16. import javax.swing.JPanel;   
  17. import javax.swing.JScrollPane;   
  18. import javax.swing.JTextArea;   
  19. import javax.swing.JTextField;   
  20.   
  21. /**  
  22.  * 查询关键词  
  23.  *   
  24.  * @author YOYO  
  25.  *   
  26.  */  
  27. public class SearchUtil {   
  28.   
  29.     /*  
  30.      * 海峡人才网URL  
  31.      */  
  32.     private static final String rqurl = "http://app.hxrc.com/services/rcjlh/jlhxx.aspx?id=1&rq=";   
  33.     private static final String twurl = "http://app.hxrc.com/services/rcjlh/twxx.aspx?twid=";   
  34.   
  35.     private static final String contentLeft = "<span class=/"p9/">";   
  36.     private static final String contentRight = "<a href=/"javascript:window.close();/">关闭窗口<br>";   
  37.     private static final String pageTotalRight = "</font></a>  ]<a href=/"jlhxx.aspx?rq=&viewpage=1&id=1";   
  38.     private static final String IDLeft = "twxx.aspx?twid=";   
  39.     private static final String IDRight = "/" target=/"_blank/">详细资料";   
  40.     private static final String companyNameLeft = "<span class=/"p9/"><font color =/"black/">";   
  41.   
  42.     public static String search(int startID, int endID, String[] keys)   
  43.             throws MalformedURLException, IOException {   
  44.         StringBuffer ret = new StringBuffer("");   
  45.   
  46.         int[] keyCount = new int[keys.length];   
  47.   
  48.         // 打开和URL之间的连接   
  49.         HttpURLConnection conn;   
  50.         BufferedReader in = null;   
  51.         StringBuffer result;   
  52.   
  53.         for (int i = startID, j = 1; i <= endID; ++i, ++j) {   
  54.             result = new StringBuffer("");   
  55.   
  56.             conn = (HttpURLConnection) new URL(twurl + String.valueOf(i))   
  57.                     .openConnection();   
  58.   
  59.             in = new BufferedReader(new InputStreamReader(   
  60.                     conn.getInputStream(), "UTF-8"));   
  61.   
  62.             String line;   
  63.             while ((line = in.readLine()) != null) {   
  64.                 result.append("/n" + line);   
  65.             }   
  66.   
  67.             String context = result.toString().toLowerCase();   
  68.             context = context.substring(context.indexOf(contentLeft)   
  69.                     + contentLeft.length(), context.indexOf(contentRight));   
  70.   
  71.             if (context.contains("对不起,没有满足条件的记录")) {   
  72.                 --j;   
  73.             }   
  74.   
  75.             // 处理公司姓名   
  76.             int companyNameIndex = context.indexOf(companyNameLeft)   
  77.                     + companyNameLeft.length();   
  78.             String companyName = context.substring(companyNameIndex, context   
  79.                     .indexOf("/n", companyNameIndex + 1));   
  80.             companyName = companyName.replaceAll(" "" ").trim();   
  81.             if (companyName.length() > 13) {   
  82.                 companyName = companyName.substring(013);   
  83.             }   
  84.   
  85.             for (int k = 0; k < keys.length; ++k) {   
  86.                 if (context.contains(keys[k].toLowerCase())) {   
  87.                     ++keyCount[k];   
  88.                     ret.append("摊位号/"" + j + "/"【" + companyName + "】 包含关键词["  
  89.                             + keys[k] + "]。/n");   
  90.                 }   
  91.             }   
  92.   
  93.             in.close();   
  94.             conn.disconnect();   
  95.         }   
  96.   
  97.         ret.append("总共检索" + (endID - startID + 1) + "条记录,其中:/n");   
  98.         for (int k = 0; k < keys.length; ++k) {   
  99.             ret.append("关键词[" + keys[k] + "]出现了 " + keyCount[k] + " 次。/n");   
  100.         }   
  101.   
  102.         return ret.toString();   
  103.     }   
  104.   
  105.     public static String searchRQ(int rq, String[] keys)   
  106.             throws MalformedURLException, IOException {   
  107.   
  108.         // 打开和URL之间的连接   
  109.         HttpURLConnection conn = (HttpURLConnection) new URL(rqurl   
  110.                 + String.valueOf(rq) + "&viewpage=0").openConnection();   
  111.         StringBuffer result = new StringBuffer("");   
  112.   
  113.         BufferedReader in = new BufferedReader(new InputStreamReader(conn   
  114.                 .getInputStream(), "UTF-8"));   
  115.   
  116.         String line;   
  117.         while ((line = in.readLine()) != null) {   
  118.             result.append("/n" + line);   
  119.         }   
  120.   
  121.         in.close();   
  122.         conn.disconnect();   
  123.   
  124.         String context = result.toString();   
  125.         int startID = Integer.parseInt(context.substring(context   
  126.                 .indexOf(IDLeft)   
  127.                 + IDLeft.length(), context.indexOf(IDRight)));   
  128.   
  129.         String pageTotalRight = SearchUtil.pageTotalRight.substring(037) + rq   
  130.                 + SearchUtil.pageTotalRight.substring(37);   
  131.         int pageTotalIndex = context.indexOf(pageTotalRight) - 1;   
  132.         int pageTotal;   
  133.         if (pageTotalIndex > 0) {   
  134.             pageTotal = Integer.parseInt(context.substring(pageTotalIndex,   
  135.                     pageTotalIndex + 1));   
  136.         } else {   
  137.             pageTotal = 1;   
  138.         }   
  139.   
  140.         conn = (HttpURLConnection) new URL(rqurl + String.valueOf(rq)   
  141.                 + "&viewpage=" + String.valueOf(pageTotal - 1))   
  142.                 .openConnection();   
  143.         in = new BufferedReader(new InputStreamReader(conn.getInputStream(),   
  144.                 "UTF-8"));   
  145.   
  146.         result = new StringBuffer("");   
  147.         while ((line = in.readLine()) != null) {   
  148.             result.append("/n" + line);   
  149.         }   
  150.   
  151.         in.close();   
  152.         conn.disconnect();   
  153.   
  154.         context = result.toString();   
  155.   
  156.         int endID = Integer.parseInt(context.substring(context   
  157.                 .lastIndexOf(IDLeft)   
  158.                 + IDLeft.length(), context.lastIndexOf(IDRight)));   
  159.   
  160.         return search(startID, endID, keys);   
  161.     }   
  162.   
  163.     /**  
  164.      * @param args  
  165.      * @throws IOException  
  166.      */  
  167.     @SuppressWarnings("serial")   
  168.     public static void main(String[] args) throws Exception {   
  169.         new JFrame() {   
  170.   
  171.             private JTextField rqField = new JTextField("");   
  172.   
  173.             private JTextField keyField = new JTextField("");   
  174.   
  175.             private JTextArea contextArea = new JTextArea("");   
  176.   
  177.             private JScrollPane scrollPane = new JScrollPane(contextArea);   
  178.   
  179.             {   
  180.                 scrollPane   
  181.                         .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);   
  182.                 scrollPane   
  183.                         .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);   
  184.   
  185.                 this.setTitle("查询海峡人才网人才交流会招聘信息");   
  186.                 this.setSize(350500);   
  187.                 this.setLayout(new BorderLayout());   
  188.                 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  189.   
  190.                 this.add(new JPanel() {   
  191.                     {   
  192.                         this.setLayout(new GridLayout(22));   
  193.   
  194.                         this.add(new JLabel("人才交流会编号:"));   
  195.                         this.add(rqField);   
  196.   
  197.                         this.add(new JLabel("关键词(/"|/"分隔):"));   
  198.                         this.add(keyField);   
  199.                     }   
  200.                 }, BorderLayout.NORTH);   
  201.   
  202.                 this.add(scrollPane, BorderLayout.CENTER);   
  203.   
  204.                 this.add(new JButton("开始查询") {   
  205.                     {   
  206.                         this.addActionListener(new ActionListener() {   
  207.   
  208.                             @Override  
  209.                             public void actionPerformed(ActionEvent e) {   
  210.                                 if (keyField.getText().trim().length() == 0) {   
  211.                                     JOptionPane.showMessageDialog(null"请输入关键字!");   
  212.                                     return;   
  213.                                 }   
  214.                                 try {   
  215.                                     int rq = Integer.valueOf(rqField.getText());   
  216.                                     String keys = keyField.getText();   
  217.                                     contextArea.setText(searchRQ(rq, keys   
  218.                                             .split("//|")));   
  219.                                 } catch (Exception e1) {   
  220.                                     e1.printStackTrace();   
  221.                                 }   
  222.                             }   
  223.                         });   
  224.                     }   
  225.                 }, BorderLayout.SOUTH);   
  226.             }   
  227.         }.setVisible(true);   
  228.     }   
  229. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值