4

この Web サイトにログインしようとしています: http://deeproute.com

これは私のコードです。

            Connection.Response res = null;
            Connection homeConnection = null;
            Document homePage = null;
            Map<String, String> loginCookies = null;
            try {
                res = Jsoup.connect("http://www.deeproute.com/")
                        .data("cookieexists", "false")
                        .data("name", user)
                        .data("password", pswd).method(Method.POST)
                        .execute();

            } catch (IOException e) {

                e.printStackTrace();
            }

            if (res != null) {

                loginCookies = res.cookies();

                try {
                    homePage = Jsoup.connect("http://www.deeproute.com")
                            .cookies(loginCookies).get();
                } catch (IOException e) {
                    e.printStackTrace();
                }

残念ながら、これは同じページをログインしていない状態で返すだけです。私は何を間違っていますか?

4

2 に答える 2

6

投稿する前にフォームを読む必要があります。param subbera=Login がありません。


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

    Connection.Response loginForm = Jsoup.connect("http://deeproute.com/deeproute/default.asp")
            .method(Connection.Method.GET)
            .execute();

    Document document = Jsoup.connect("http://deeproute.com/deeproute/default.asp")
            .data("cookieexists", "false")
            .data("name", "username")
            .data("password", "pass")
            .data("subbera", "Login")
            .cookies(loginForm.cookies())
            .post();

}
于 2013-04-20T22:55:39.723 に答える
0

数か月(実際には数か月)遅すぎるかもしれませんが、サイトが本当に混乱するように設計されている場所(データを抽出する必要があるサイト)で、Webスクレイピングを行う必要があることがわかりました。また、Cookie 情報を取得するために、最初にログインする必要がありました。@MariuszSの答えは役に立ちましたが、唯一の問題は、予想されるマップ/キー値ペアがどうあるべきかわからなかったことです。Javascript のおかげで、フォームのキーと値をすばやく取得し、正常にログインできました。Javascript は次のとおりです。

var str = "";

//the form selector i.e.
//that which is to be submitted to. In
//my case, i needed to submit the entire body
var arr = $("input[name]");

for(var i = 0, l = arr.length; i<l; i++){
str+= '.data("'+ arr[i].name +'", "'+ arr[i].value +'")'
}

「str」の値を Jsoup リクエストの前に追加します。

.method(Connection.Method.GET)
.execute();

これが私のように多少役立つことを願っています:)

于 2014-06-11T16:27:50.190 に答える