2012-05-09 16 views
4

を解析:オープンJsoupとの接続には、ステータスコードを取得し、私は次の操作を行いますjsoupを使用してクラスを作成していた文書

  1. コンストラクタはURLへの接続をオープンします。
  2. ページのステータスを確認する方法があります。すなわち200、404など
  3. 私はページを解析し、URLのリストを返すメソッドを持っている。#

以下は、私のようにその非常にラフではない、私がやろうとしています何の大まかな作業であります「私は、私が文書を解析して取得する方法がわからないあなたは、私がページのステータスを取得することができます見ることができるようにさまざまなものがたくさんに

public class ParsePage { 
private String path; 
Connection.Response response = null; 

private ParsePage(String langLocale){ 
    try { 
     response = Jsoup.connect(path) 
       .userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21") 
       .timeout(10000) 
       .execute(); 
    } catch (IOException e) { 
     System.out.println("io - "+e); 
    } 
} 

public int getSitemapStatus(){ 
    int statusCode = response.statusCode(); 
    return statusCode; 
} 

public ArrayList<String> getUrls(){ 
    ArrayList<String> urls = new ArrayList<String>(); 

} 
} 

をしようとしますが、コンストラクタから、すでに開いている接続を使用してきまし試してみました:

Document doc = connection.get(); 

しかし、それはないです。助言がありますか?これについてもっと良い方法がありますか?

答えて

9

は、Documentとして応答者の身体を解析し、それを返すparse()方法があります。 これを持っていれば、何でもできます。例えば

、あなたは、使用してログインする必要がない場合getUrls()

public class ParsePage { 
    private String path; 
    Connection.Response response = null; 

    private ParsePage(String langLocale){ 
     try { 
     response = Jsoup.connect(path) 
      .userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21") 
      .timeout(10000) 
      .execute(); 
     } catch (IOException e) { 
     System.out.println("io - "+e); 
     } 
    } 

    public int getSitemapStatus() { 
     int statusCode = response.statusCode(); 
     return statusCode; 
    } 

    public ArrayList<String> getUrls() { 
     ArrayList<String> urls = new ArrayList<String>(); 
     Document doc = response.parse(); 
     // do whatever you want, for example retrieving the <url> from the sitemap 
     for (Element url : doc.select("url")) { 
     urls.add(url.select("loc").text()); 
     } 
     return urls; 
    } 
} 
+0

も次のようになりますあなたの接続

if (200 == response.statusCode()) { doc = Jsoup.connect(" Your URL").get(); Elements elements = doc.select("href"); /* what ever you want to do*/ } 

を作成することができます

Response response = Jsoup.connect("Your Url ").followRedirects(false).execute(); System.out.println(response.statusCode() + " : " + response.url()); 

response.statusCode()は、ステータスコード

戻ります、ありがとう! – Peck3277

2

応答オブジェクトに対してparse()を呼び出すことができるはずです。 Connection.ResponseタイプのJSoupドキュメントに記載されているとおり

Document doc = response.parse(); 
6

の実装を参照してください。

Document doc = Jsoup.connect("url").get(); 

あなたは、私が使用してお勧めするログインする必要がある場合:

Response res = Jsoup.connect("url") 
    .data("loginField", "yourUser", "passwordField", "yourPassword") 
    .method(Method.POST) 
    .execute(); 
Document doc = res.parse(); 

//If you need to keep logged in to the page, use 
Map<String, String> cookies = res.cookies; 

//And by every consequent connection, you'll need to use 
Document pageWhenAlreadyLoggedIn = Jsoup.connect("url").cookies(cookies).get(); 

URLを取得するための使用方法で、おそらく試してみます

それについてです
Elements elems = doc.select(a[href]); 
for (Element elem : elems) { 
    String link = elem.attr("href"); 
} 

..

1

良い仕事を続けて、あなたがjsoupとの接続を作りたいように、ステータスコードをチェックして、ステータスコードに応じてあなたが解析されますか、あなたが好きなあなたの状況に思えますする。

まず、接続を作成する代わりに、URLのステータスコードを確認する必要があります。その後、あなたのクラスはかなりうまくそれを解決し、この

package com.demo.soup.core; 

import java.io.IOException; 

import org.jsoup.Connection.Response; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 

/** 
* The Class DemoConnectionWithJsoup. 
* 
* @author Ankit Sood Apr 21, 2017 
*/ 
public class DemoConnectionWithJsoup { 

    /** 
    * The main method. 
    * 
    * @param args 
    *   the arguments 
    */ 
    public static void main(String[] args) { 
    Response response; 
    try { 
     response = Jsoup.connect("Your URL ").followRedirects(false).execute(); 

     /* response.statusCode() will return you the status code */ 
     if (200 == response.statusCode()) { 
     Document doc = Jsoup.connect("Your URL").get(); 

     /* what ever you want to do */ 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    } 

} 
関連する問題