简体   繁体   English

使用jsoup登录网站

[英]Login to website using jsoup

I tried to look everywhere to find a solution for my problem without any success. 我试图到处寻找解决问题的方法,但没有成功。 I'm trying to login to http://www.dailystrength.org/ website, however, my code doesn't work properly since I'm seeing that my crawler can't access some users' profiles that are private for non-registered members. 我正在尝试登录http://www.dailystrength.org/网站,但是我的代码无法正常运行,因为我发现自己的抓取工具无法访问某些非个人用户专用的个人资料注册会员。

Here is my code: 这是我的代码:

String loginUrl = source + "/index.php?option=com_comprofiler&task=login";
Document login = Jsoup.connect(loginUrl).timeout(20000).get();
Map<String, String> parameters = new HashMap<String, String>();
Elements inputs = login.select("input");

for(Element input : inputs)
{
    parameters.put(input.attr("name"), input.attr("value"));
}

parameters.put("username", "user");
parameters.put("passwd", "pass");
parameters.put("remember", "yes");

cookies = Jsoup.connect(loginUrl).data(parameters).method(org.jsoup
          .Connection.Method.POST).timeout(20000).execute().cookies();

I tried also to include hidden input values, but it didn't work too. 我也尝试包含隐藏的输入值,但是它也没有用。 Any help would be much appreciated. 任何帮助将非常感激。

Thank you 谢谢

Hey you can use Htmlunit and jsoup. 嘿,您可以使用Htmlunit和jsoup。 For login use Htmlunit, its easy, you just need to submit form with username and password, after login, new page will come and you can deal with jsoup with that new page. 使用Htmlunit进行登录非常简单,只需提交带有用户名和密码的表单,登录后就会出现新页面,您可以用该新页面处理jsoup。

Is this what u were trying to do 这是你想做的

String loginUrl = "http://www.dailystrength.org/" + "/index.php?option=com_comprofiler&task=login";
    Document login = Jsoup.connect(loginUrl).timeout(20000).header("username", "user").header("passwd", "pass").header("remember", "yes").post();
    Map<String, String> parameters = new HashMap<String, String>();
    Elements inputs = login.select("input");
for(Element input : inputs)
{
    String thi= input.attr("name");
    String  thi2=input.attr("value");
    System.out.println("THIS" + thi);
    System.out.println("THIS2" + thi2);
parameters.put(input.attr("name"), input.attr("value"));
}

Hope this works for u 希望这对你有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM