2016-12-15 41 views
1

私はJsoupでフォーラムページを読もうとしていますが、できません。私は最初のページまたはリストページを読むことができるよりも、首尾よくログインしています。しかし、私は、スレッドのページに移動するとき、それは私に403を与えている。ここのコードです:JSOUPで成功した後にスレッドページを読むことができません

Connection.Response loginForm = Jsoup.connect("http://picturepub.net/index.php?login/login").method(Connection.Method.GET) 
    .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0").timeout(0).execute(); 

Document doc = Jsoup.connect("http://picturepub.net/index.php?login/login").data("cookieexists", "false").data("cookie_check", "1").data("login", "swordblazer") 
    .data("password", "picturepub").data("register", "0").data("redirect", "/index.php").cookies(loginForm.cookies()) 
    .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0").post(); 

doc = loginForm.parse(); 

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

List<String> urls = new ArrayList<String>(); 
List<String> threadUrls = new ArrayList<String>(); 
int h = 0; 
for (int i = 1; i < 20; i++) { 
    if (i == 1) 
    doc = Jsoup.connect("http://picturepub.net/index.php?forums/photoshoots-magazines.51/") 
     .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0").cookies(cookies).get(); 
    else 
    doc = Jsoup.connect("http://picturepub.net/index.php?forums/photoshoots-magazines.51/page-" + i) 
     .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0").cookies(cookies).get(); 

    // get all links 
    Elements links = doc.select("a[href]"); 
    System.out.println(doc.title()); 
    for (Element element : links) { 
    if (element.absUrl("href").contains("threads")) { 
     String linkImage = element.absUrl("href"); 
     Document document = Jsoup.connect(linkImage).cookies(cookies).get(); 

     if (!threadUrls.contains(linkImage)) { 
     threadUrls.add(linkImage); 
     h++; 
     } 

    } 
    } 
} 
+0

パラメータ/クッキーが不足していると思われるため、おそらく '403'が表示されます。ログイン方法を理解している場合は、同じ方法を使用してブラウザとサイト間のトラフィックを監視し、ブラウザが送信している内容を確認するよりも、 – TDG

+0

私はそれをしました。私がサーバーに送る必要のあるクッキー以外のものはありますか? – user236928

+0

クッキーと必要なパラメーター。 – TDG

答えて

0

JSoup接続はお互いに無関係なので、「ログイン状態/セッション」を共有していません。それらの間で状態を慎重に複製する必要があります。

  • loginForm応答が認証Cookieを返さない、とあなたはしかし、あなたが後でこれらのクッキーを使用し、承認されたリソースのためにそれらを使用することはできません:あなたはので、いくつかの理由でHTTP 403を得ています。
  • 認証Cookieを取得するには、POST http://picturepub.net/index.php?login/login応答からCookieを取得し、それをドキュメントに変換しないでください。 2番目の要求は、method(POST)を使用してPOST要求として宣言する必要があります。
  • 失敗した要求Jsoup.connect(linkImage).cookies(cookies).get();は、User-Agentが逃げます。エラーが発生しやすいコードが少ないようにするために

、あなたはあなたのコードがより堅牢になるだろうものの一つとして、リファクタリングを検討すべきです。以前JSoupバージョンは、そのウェブサイトで使用されHTTP 307 Temporary Redirectを扱うことができないので

private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0"; 
private static final String BASE_URL = "http://picturepub.net/index.php"; 
private static final int PAGE_COUNT = 20; 

static void grab() 
     throws IOException { 
    out.println("Getting the login form..."); 
    final Response getLoginFormResponse = prepareConnection(GET, "?login/login", emptyMap()) 
      .execute(); 
    out.println("Posting the login data..."); 
    // Avoid converting to document when it's unnecessary and use `execute()` 
    final Response postLoginFormResponse = prepareConnection(POST, "?login/login", getLoginFormResponse.cookies()) 
      .data("cookieexists", "false") 
      .data("cookie_check", "1") 
      .data("login", ...YOUR USERNAME...) 
      .data("password", ...YOUR PASSWORD...) 
      .data("register", "0") 
      .data("redirect", "/index.php") 
      .execute(); 
    // Obtain the authentication cookies 
    final Map<String, String> cookies = postLoginFormResponse.cookies(); 
    // If you want to discard duplicates, just don't use lists -- sets are designed for unique elements. 
    // The `h` is unnecessary because you can query the collection for its size: threadUrls.size() 
    final Collection<String> threadUrls = new LinkedHashSet<>(); 
    for (int i = 1; i <= PAGE_COUNT; i++) { 
     out.printf("Page #%d...\n", i); 
     final Document getPageDocument = prepareConnection(GET, "?forums/photoshoots-magazines.51/" + (i == 1 ? "" : "page-" + i), cookies) 
       .execute() 
       .parse(); 
     out.printf("Page #%d: %s\n", i, getPageDocument.title()); 
     // `a[href*=threads/]` is a selector to obtain all links having the "threads/" in <A> element URLs -- no need to check for substring later 
     // The following code uses Java 8 streams to filter out duplicate links on the page 
     final Iterable<String> hrefs = getPageDocument.select("a[href*=threads/]") 
       .stream() 
       .map(e -> e.absUrl("href")) 
       .collect(toSet()); 
     for (final String href : hrefs) { 
      out.printf("Probing: %s ... ", href); 
      final Response analyzeMeResponse = prepareConnection(GET, stripBaseUrl(href), cookies) 
        .execute(); 
      threadUrls.add(href); 
      out.println("Done!"); 
     } 
    } 
    out.println(threadUrls); 
} 

private static String stripBaseUrl(final String url) 
     throws IllegalArgumentException { 
    if (!url.startsWith(BASE_URL)) { 
     // This must not happen for a well-written parser 
     throw new IllegalArgumentException(url); 
    } 
    return url.substring(BASE_URL.length()); 
} 

// Just make sure that a particular connection is: 
// * bound to the BASE_URL defined above 
// * bound to a specific HTTP method 
// * follows redirects 
// * User-Agent is set 
// * cookies are always set 
private static Connection prepareConnection(final Method method, final String url, final Map<String, String> cookies) { 
    return connect(BASE_URL + url) 
      .followRedirects(true) 
      .method(method) 
      .userAgent(USER_AGENT) 
      .cookies(cookies); 
} 

コードは、上記org.jsoup:json:1.10.1に基づいています。

関連する問題