• Webclent基本操作


    /**   
    * @Title: webclientTest.java 
    * @Package webclient 
    * @Description: TODO(用一句话描述该文件做什么) 
    * @author A18ccms A18ccms_gmail_com   
    * @date 2016年8月30日 下午3:52:41 
    * @version V1.0   
    */
    package webclient;
    
    import java.io.IOException;
    import java.net.MalformedURLException;
    
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    
    import com.gargoylesoftware.htmlunit.BrowserVersion;
    import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
    import com.gargoylesoftware.htmlunit.WebClient;
    import com.gargoylesoftware.htmlunit.html.HtmlButton;
    import com.gargoylesoftware.htmlunit.html.HtmlForm;
    import com.gargoylesoftware.htmlunit.html.HtmlPage;
    
    /**
     * @ClassName: webclientTest
     * @Description: Webclent基本操作
     * @author zeze
     * @date 2016年8月30日 下午3:52:41
     * 
     */
    public class webclientTest {
    
        /**
         * 
         * @Title: main
         * @param @param
         *            args 设定文件
         * @return void 返回类型
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            WebClient webClient = new WebClient(BrowserVersion.CHROME);
            webClient.getOptions().setTimeout(20000);
            webClient.getCookieManager().setCookiesEnabled(true);
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setCssEnabled(false);
            webClient.getOptions().setJavaScriptEnabled(false);
            webClient.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) "
                    + "AppleWebKit/537.36 (KHTML, like Gecko) " + "Chrome/43.0.2357.134 Safari/537.36");
            webClient.addRequestHeader("Accept",
                    "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            webClient.addRequestHeader("Accept-Encoding", "gzip, deflate, sdch");
            webClient.addRequestHeader("Accept-Language", "zh-CN,zh;q=0.8");
            webClient.addRequestHeader("Connection", "keep-alive");
            webClient.addRequestHeader("Host", "www.cnblogs.com");
            // webClient.addRequestHeader("", "");
            HtmlPage page = null;
            try {
                String Url = "http://www.cnblogs.com/zeze";
                page = webClient.getPage(Url);
                Document doc = Jsoup.parse(page.asXml());
                System.out.println(doc);
                while (doc.select("title").text().equals("Robot Check")) {
                    HtmlForm form = page.getForms().get(0);
                    HtmlButton button = (HtmlButton) form.getElementsByTagName("button").get(0);
                    form.getInputByName("field-keywords").setValueAttribute("");
                    doc = Jsoup.parse(page.asXml());
                    try {
                        page = button.click();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            } catch (FailingHttpStatusCodeException e) {
    
            } catch (MalformedURLException e) {
    
            } catch (IOException eq) {
    
            }
        }
    
    }
  • 相关阅读:
    读书笔记:你就是极客软件开发人员生存指南
    读书笔记:重来 Rework
    敏捷个人2012.1月份线下活动报道:谈谈职业
    敏捷个人2011.12月份线下活动报道:认识自我
    敏友的【敏捷个人】有感(12): 敏友们自发组织的线上思想的碰撞
    敏捷团队:我尽力先做好本职工作是否正确?
    OpenExpressApp:精通 WPF UI Virtualization
    MDSF:发布图形编辑器源码OpenGraphicEditor
    产品管理:用户访谈之道
    敏捷个人架构图
  • 原文地址:https://www.cnblogs.com/zeze/p/5822448.html
Copyright © 2020-2023  润新知