HtmlUnit 获取登录后的页面信息失败

本文介绍使用HtmlUnit进行网页登录并抓取数据的过程。通过设置WebClient参数,实现JavaScript执行及登录功能。但登录后页面数据抓取存在问题。

最近在学习网络爬虫抓取数据, 运用HtmlUnit, 可以获取到动态加载后的数据。但是有些网站需要先登录,后获取登录后的数据就出现问题。

public static void TianyaTestByHtmlUnit() {
 
        try {
            WebClient webClient = new WebClient(BrowserVersion.FIREFOX_45);
 
            // The ScriptException is raised because you have a syntactical
            // error in your javascript.
            // Most browsers manage to interpret the JS even with some kind of
            // errors
            // but HtmlUnit is a bit inflexible in that sense.
            webClient.getOptions().setUseInsecureSSL(true);
            webClient.getOptions().setJavaScriptEnabled(true); 
            webClient.getOptions().setCssEnabled(false); 
            webClient.setAjaxController(new NicelyResynchronizingAjaxController());
            webClient.getOptions().setRedirectEnabled(true);
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getOptions().setThrowExceptionOnScriptError(false); 
            webClient.setJavaScriptTimeout(20000);
            webClient.waitForBackgroundJavaScript(10000);
            webClient.getOptions().setRedirectEnabled(true);
    webClient.getCookieManager().setCookiesEnabled(true);
 
            // get the url
            HtmlPage page = webClient.getPage("http://passport.tianya.cn/login.jsp");
            System.out.println("Orgin page data =" + page.asXml());


             HtmlTextInput username = (HtmlTextInput) page.getElementById("userName");
             username.type("lms_test_****");
             HtmlPasswordInput password = (HtmlPasswordInput) page.getElementById("password");
             password.click();
             password.type("liu*****");
             //HtmlAnchor submit =  page.getAnchorByName("loginBtn");
             HtmlButton submit = (HtmlButton) page.getElementById("loginBtn");
             webClient.waitForBackgroundJavaScript(4000);
             HtmlPage nextPage =(HtmlPage) submit.click();
             
            
            // Wait js load the data
            webClient.waitForBackgroundJavaScript(10000);
           
            Thread.sleep(20000);
            System.out.println("After click login button =" + nextPage.asXml());   
             
            Set<Cookie> cookies = webClient.getCookieManager().getCookies();;   
            Map<String, String> responseCookies = new HashMap<String, String>();  
            for (Cookie c : cookies) {  
                responseCookies.put(c.getName(), c.getValue());  
                System.out.println("cookie name --" + c.getName()+" value:"+c.getValue());  
            }  
              
            webClient.close(); 
        } catch (Exception e) {
 
            e.printStackTrace();
        }
    }
 

nextPage.asXml() 获取的数据总是与登录前的数据差不多,求大神帮忙解决!

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值