最近对iteye举办的问答大赛非常着迷,总想为更多的人答疑解惑。得到别人的点赞,荣誉感瞬间爆棚。这也折射出程序员这个职业的弊端。虽然问题大多都是初级水平,毕竟不懂才问的啊,闻到有先后,术业有专攻。那些大神们对这个很不屑,觉得拉低了他们的智商。这个我不很赞同,好了就这么多了把……
通过问答,我发现了一款很好的工具,记录下来,以后可能会有用武之地。
【htmlUnit】
htmlunit 是一款开源的java 页面分析工具,读取页面后,可以有效的使用htmlunit分析页面上的内容。项目可以模拟浏览器运行,被誉为java浏览器的开源实现。这个没有界面的浏览器,运行速度也是非常迅速的。可以作为小型的爬虫框架。
下面是他的代码
import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.Arrays; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; import com.gargoylesoftware.htmlunit.HttpMethod; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.WebRequest; import com.gargoylesoftware.htmlunit.html.HtmlForm; import com.gargoylesoftware.htmlunit.html.HtmlInput; import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; import com.gargoylesoftware.htmlunit.util.NameValuePair; public class MySina { private WebClient client; private WebRequest request; private String sinaLoginUrl = " http://mail.sina.com.cn/cgi-bin/login.php"; private String hostSinaUrl = ""; public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException { String username = "***"; String password = "***"; String newpassword = "***"; String nickname = "***"; MySina mySina = new MySina(); if (mySina.mailLoginBySina(username, password)) { // 登录 mySina.updatePwdBySina(password, newpassword); // 修改密码 mySina.updateNickName(nickname); // 修改帐户昵称 } else { System.out.println("登录失败!请检查用户名和密码是否正确!"); } } public MySina(){ client = new WebClient(BrowserVersion.INTERNET_EXPLORER_ ; client.setJavaScriptEnabled(false); } /** * 更改帐户昵称 * * @param nickname 昵称 * @return boolean * @throws FailingHttpStatusCodeException * @throws IOException */ public boolean updateNickName(String nickname) throws FailingHttpStatusCodeException, IOException { String sinaSetUrl = hostSinaUrl + "basic/setting_account"; request = new WebRequest(new URL(sinaSetUrl), HttpMethod.POST); request.setCharset("utf- ); request.setRequestParameters(Arrays.asList(new NameValuePair("nickname", nickname), new NameValuePair("pop , "on"), new NameValuePair("imap", "on"))); client.getPage(request); HtmlPage p = client.getPage(hostSinaUrl + "classic/index.php"); if (p.getBody().getTextContent().indexOf("\"NickName\":\"" + nickname + "\"") > 0) { return true; } else { return false; } } /** * 修改密码 * * @param oldpassword 旧密码 * @param newpassword 新密码 * @return boolean * @throws FailingHttpStatusCodeException * @throws IOException */ public boolean updatePwdBySina(String oldpassword, String newpassword) throws FailingHttpStatusCodeException, IOException { String sinaSetUrl = " http://login.sina.com.cn/member/security/password.php"; request = new WebRequest(new URL(sinaSetUrl), HttpMethod.POST); request.setCharset("gbk"); request.setRequestParameters(Arrays.asList(new NameValuePair("pass", oldpassword), new NameValuePair("pass , newpassword), new NameValuePair("pass , newpassword))); HtmlPage p = client.getPage(request); if (p.getBody().getTextContent().indexOf("您的密码修改成功") > 0) { return true; } else { return false; } } /** * 登录 * * @param username 用户名 * @param password 密码 * @return boolean * @throws FailingHttpStatusCodeException * @throws MalformedURLException * @throws IOException */ public boolean mailLoginBySina(String username, String password) throws FailingHttpStatusCodeException, MalformedURLException, IOException { HtmlPage loginPage = client.getPage(sinaLoginUrl); HtmlForm loginForm = loginPage.getFormByName("free"); HtmlInput u = loginForm.getInputByName("u"); HtmlInput psw = loginForm.getInputByName("psw"); HtmlSubmitInput loginButton = loginForm.getInputByName("登录"); u.setValueAttribute(username); psw.setValueAttribute(password); HtmlPage result = loginButton.click(); String resultUrl = result.getUrl().toString(); if (resultUrl.indexOf("classic/index.php") > 0) { String regex = "http://(.*?)/"; hostSinaUrl = myRegex(resultUrl, regex, null); if (hostSinaUrl.length() > 0) { return true; } else { return false; } } else { return false; } } /** * 正则匹配替换 * * @param str * @param reg * @param replace * @return */ public String myRegex(String str, String reg, String[] replace) { String result = null; Matcher m = Pattern.compile(reg).matcher(str); while (m.find()) { result = m.group(); if (replace != null && replace.length > 0) { for (String s : replace) { result = result.replace(s, ""); } } } return result; } }
【MozSwing 】
MozSwing 独立运行,不依赖客户机器的浏览器版本。(目前来看最适合我们,内核是火狐的xulrunner1.9.1),官方好几年没有升级版本了。下载地址:http://sourceforge.net/projects/mozswing/)
【JWebBrowser】
JWebBrowser 调用客户本机浏览器,通常是IE。(下载: http://sourceforge.net/projects/djproject/files/DJ%20Native%20Swing/1.0.2%20preview/DJNativeSwing-SWT-1-0-2-20111030.zip/download)
demo代码:
package chrriis.dj.nativeswing.swtimpl.demo.examples.webbrowser; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.BorderFactory; import javax.swing.JCheckBox; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import chrriis.common.UIUtils; import chrriis.dj.nativeswing.swtimpl.NativeInterface; import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser; /** * @author Christopher Deckers */ public class SimpleWebBrowserExample { public static JComponent createContent() { JPanel contentPane = new JPanel(new BorderLayout()); JPanel webBrowserPanel = new JPanel(new BorderLayout()); webBrowserPanel.setBorder(BorderFactory.createTitledBorder("Native Web Browser component")); final JWebBrowser webBrowser = new JWebBrowser(); webBrowser.navigate("http://www.google.com"); webBrowserPanel.add(webBrowser, BorderLayout.CENTER); contentPane.add(webBrowserPanel, BorderLayout.CENTER); // Create an additional bar allowing to show/hide the menu bar of the web browser. JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4)); JCheckBox menuBarCheckBox = new JCheckBox("Menu Bar", webBrowser.isMenuBarVisible()); menuBarCheckBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { webBrowser.setMenuBarVisible(e.getStateChange() == ItemEvent.SELECTED); } }); buttonPanel.add(menuBarCheckBox); contentPane.add(buttonPanel, BorderLayout.SOUTH); return contentPane; } /* Standard main method to try that test as a standalone application. */ public static void main(String[] args) { NativeInterface.open(); UIUtils.setPreferredLookAndFeel(); SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame("DJ Native Swing Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(createContent(), BorderLayout.CENTER); frame.setSize(800, 600); frame.setLocationByPlatform(true); frame.setVisible(true); } }); NativeInterface.runEventPump(); } }
参考资料: