3

サインインしたときにのみ利用できるページをクロールするために使用したいのですがJsoup、あるページにサインインして別のページに Cookie を送信する必要があることを意味していると思います。ここ
で 以前の投稿を読み、次のコードを記述します。

public static void main(String[] args) throws IOException {
    Connection.Response res = Jsoup.connect("login.yahoo.com")
        .data("login", "myusername", "passwd", "mypassword")
        .method(Method.POST)
        .execute();

Document doc=res.parse();
String sessionId = res.cookie("SESSIONID");

Document doc2 = Jsoup.connect("http://health.groups.yahoo.com/group/asthma/messages")
        .cookie("SESSIONID", sessionId)
        .get();

Elements Eles=doc2.getElementsByClass("message");

String content=Eles.first().text();

System.out.println(content);

私の質問は、ログイン情報を送信するためにここで自分の Cookie 名 (つまり、「SESSIONID」) を知る方法です。メソッドを使用して.cookies()、ログイン ページからすべての Cookie を取得しました。

B
DK
YM
T
PH
Y
F

1つずつ試してみましたが、どれもうまくいきませんでした。それらのいくつかから sessionId を取得できましたが、2 番目のページからノードを正常に取得できませんでした。つまり、正常にサインインできませんでした。どうもありがとう!

4

2 に答える 2

2

次のようなことをしようとしましたか:

Connection.Response res = Jsoup.connect("https://login.yahoo.com/config/login?")
    .data("login", "myusername", "passwd", "mypassword")
    .method(Method.POST)
    .execute();

 Map<String, String> cookies = res.cookies();

 Connection connection = Jsoup.connect("http://health.groups.yahoo.com/group/asthma/messages");

 for (Map.Entry<String, String> cookie : cookies.entrySet()) {
     connection.cookie(cookie.getKey(), cookie.getValue());     
 }

 Document doc=  connection.get();
 // #code selector
 // Example
 // Element e=doc.select(".ygrp-grdescr").first();
 // System.out.println(e.text()); // Print => This list will be for asthmatics, and anyone whose       life is affected by it. Discussions include causes, problems, and treatment

これがあなたの問題に役立つことを願っています。

于 2012-04-21T02:48:04.633 に答える