java - HtmlUnit爬虫登录搜狐网的时候重定向不成功,得到的是“登录中。。。”的页面。

 爱你一辈子2502860605 发布于 2022-11-05 07:41

代码如下:

import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;

public class Sohu {

    public static void main(String[] args) throws Exception{

        WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24);
        HtmlPage page = webClient.getPage("http://i.auto.sohu.com/user/login/toLogin.at");
        HtmlTextInput user = (HtmlTextInput) page.getElementById("pp_login");
        user.type("heiheiabcd@sohu.com");
        HtmlPasswordInput password = (HtmlPasswordInput) page.getElementById("pw_login");
        password.click();
        password.type("Xx20111111");
        HtmlAnchor submit = page.getFirstByXPath("//form[@id='user-login']/a[1]");
        page = (HtmlPage) submit.click();
        Thread.sleep(3000);
        System.out.println(page.asText());
        webClient.closeAllWindows();
    }
} 

得到的结果如下:

登录 - 我的搜狐汽车
首页  |  新闻  |  购车  |  用车  |  论坛
heiheiabcd@sohu.com Xx20111111
**登录中...       //为什么是登录中,而得不到登录后的界面,有什么办法?**
checked 下次自动登录 忘记密码?
使用其它账号登录:
意见反馈 - 搜狗输入法 - 支付中心 - 搜狐招聘 - 广告服务 - 客服中心 - 联系方式 - 保护隐私权 - About SOHU - 公司介绍
Copyright © 2011 Sohu.com Inc. All Rights Reserved. 搜狐公司 版权所有
搜狐不良信息举报电话:010-62728061 举报邮箱:jubao@contact.sohu.com
京ICP备060382号

问题:怎么才能得到登录后的页面而不是登录中的页面?
(page.getWebResponse().getContentAsString()得到页面源码。page.getUrl()得到登录中页面的url)

2 个回答
  • 线程休眠的时间再长点行不行?

    2022-11-09 09:28 回答
  • 登录中这个页面header有set-cookie吧,有的话,记录下来,用Cookie访问首页就好了

    2022-11-09 09:28 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有