乱乱的代码段。。
最近都在海峡人才网上看交流会信息:http://app.hxrc.com/services/rcjlh/jlhxx.aspx?id=1
但是它们的搜索似乎不能用?每次都要点开看企业的详细资料,好痛苦。。
今天又要招聘会了,偏偏半夜更新详细信息。
于是随意写了段乱乱的代码,没有注释,UI也随便乱写,可用就行。
界面长这样:输入人才交流会编号(在网页上点到链接的地方会看到rq=?)
import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; /** * 查询关键词 * * @author YOYO * */ public class SearchUtil { /* * 海峡人才网URL */ private static final String rqurl = "http://app.hxrc.com/services/rcjlh/jlhxx.aspx?id=1&rq="; private static final String twurl = "http://app.hxrc.com/services/rcjlh/twxx.aspx?twid="; private static final String contentLeft = "<span class=\"p9\">"; private static final String contentRight = "<a href=\"javascript:window.close();\">关闭窗口<br>"; private static final String pageTotalRight = "</font></a> ]<a href=\"jlhxx.aspx?rq=&viewpage=1&id=1"; private static final String IDLeft = "twxx.aspx?twid="; private static final String IDRight = "\" target=\"_blank\">详细资料"; private static final String companyNameLeft = "<span class=\"p9\"><font color =\"black\">"; public static String search(int startID, int endID, String[] keys) throws MalformedURLException, IOException { StringBuffer ret = new StringBuffer(""); int[] keyCount = new int[keys.length]; // 打开和URL之间的连接 HttpURLConnection conn; BufferedReader in = null; StringBuffer result; for (int i = startID, j = 1; i <= endID; ++i, ++j) { result = new StringBuffer(""); conn = (HttpURLConnection) new URL(twurl + String.valueOf(i)) .openConnection(); in = new BufferedReader(new InputStreamReader( conn.getInputStream(), "UTF-8")); String line; while ((line = in.readLine()) != null) { result.append("\n" + line); } String context = result.toString().toLowerCase(); context = context.substring(context.indexOf(contentLeft) + contentLeft.length(), context.indexOf(contentRight)); if (context.contains("对不起,没有满足条件的记录")) { --j; } // 处理公司姓名 int companyNameIndex = context.indexOf(companyNameLeft) + companyNameLeft.length(); String companyName = context.substring(companyNameIndex, context .indexOf("\n", companyNameIndex + 1)); companyName = companyName.replaceAll(" ", " ").trim(); if (companyName.length() > 13) { companyName = companyName.substring(0, 13); } for (int k = 0; k < keys.length; ++k) { if (context.contains(keys[k].toLowerCase())) { ++keyCount[k]; ret.append("摊位号\"" + j + "\"【" + companyName + "】 包含关键词[" + keys[k] + "]。\n"); } } in.close(); conn.disconnect(); } ret.append("总共检索" + (endID - startID + 1) + "条记录,其中:\n"); for (int k = 0; k < keys.length; ++k) { ret.append("关键词[" + keys[k] + "]出现了 " + keyCount[k] + " 次。\n"); } return ret.toString(); } public static String searchRQ(int rq, String[] keys) throws MalformedURLException, IOException { // 打开和URL之间的连接 HttpURLConnection conn = (HttpURLConnection) new URL(rqurl + String.valueOf(rq) + "&viewpage=0").openConnection(); StringBuffer result = new StringBuffer(""); BufferedReader in = new BufferedReader(new InputStreamReader(conn .getInputStream(), "UTF-8")); String line; while ((line = in.readLine()) != null) { result.append("\n" + line); } in.close(); conn.disconnect(); String context = result.toString(); int startID = Integer.parseInt(context.substring(context .indexOf(IDLeft) + IDLeft.length(), context.indexOf(IDRight))); String pageTotalRight = SearchUtil.pageTotalRight.substring(0, 37) + rq + SearchUtil.pageTotalRight.substring(37); int pageTotalIndex = context.indexOf(pageTotalRight) - 1; int pageTotal; if (pageTotalIndex > 0) { pageTotal = Integer.parseInt(context.substring(pageTotalIndex, pageTotalIndex + 1)); } else { pageTotal = 1; } conn = (HttpURLConnection) new URL(rqurl + String.valueOf(rq) + "&viewpage=" + String.valueOf(pageTotal - 1)) .openConnection(); in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); result = new StringBuffer(""); while ((line = in.readLine()) != null) { result.append("\n" + line); } in.close(); conn.disconnect(); context = result.toString(); int endID = Integer.parseInt(context.substring(context .lastIndexOf(IDLeft) + IDLeft.length(), context.lastIndexOf(IDRight))); return search(startID, endID, keys); } /** * @param args * @throws IOException */ @SuppressWarnings("serial") public static void main(String[] args) throws Exception { new JFrame() { private JTextField rqField = new JTextField(""); private JTextField keyField = new JTextField(""); private JTextArea contextArea = new JTextArea(""); private JScrollPane scrollPane = new JScrollPane(contextArea); { scrollPane .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scrollPane .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); this.setTitle("查询海峡人才网人才交流会招聘信息"); this.setSize(350, 500); this.setLayout(new BorderLayout()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(new JPanel() { { this.setLayout(new GridLayout(2, 2)); this.add(new JLabel("人才交流会编号:")); this.add(rqField); this.add(new JLabel("关键词(\"|\"分隔):")); this.add(keyField); } }, BorderLayout.NORTH); this.add(scrollPane, BorderLayout.CENTER); this.add(new JButton("开始查询") { { this.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (keyField.getText().trim().length() == 0) { JOptionPane.showMessageDialog(null, "请输入关键字!"); return; } try { int rq = Integer.valueOf(rqField.getText()); String keys = keyField.getText(); contextArea.setText(searchRQ(rq, keys .split("\\|"))); } catch (Exception e1) { e1.printStackTrace(); } } }); } }, BorderLayout.SOUTH); } }.setVisible(true); } }
因为是所有数据都读完后才一次输出,因此遇到大型招聘会的时候会卡一下。。
还有就是懒得读公司名,于是直接取招聘详细信息的第一行为名。
- 无匹配