2017-04-15 24 views
1

JSOUPでWebページにアクセスしようとしましたが、UTF-8文字に問題があります。 これは、特殊文字を使用しないULRには問題ありません。JSOUP UTF-8文字を接続

String linkTeam = "https://www.fifaindex.com/de/team/" + team.getId() + "/" + URLEncoder.encode(team.getName().replaceAll(" ", ""),"UTF-8"); 
    System.out.println(linkTeam); 
    String name = URLEncoder.encode(team.getName().replaceAll(" ", ""), "UTF-8"); 
    System.out.println(name); 
    Document doc = Jsoup.connect(linkTeam).get(); 
    Elements strength = doc.getElementsByClass("badge r3"); 
    team.setSturm(Integer.parseInt(strength.get(0).text())); 
    team.setMittelfeld(Integer.parseInt(strength.get(1).text())); 
    team.setAbwehr(Integer.parseInt(strength.get(2).text())); 
    return team; 

これは、UTF-8文字を含むURLの出力です:私はJsoup.connectを使用する場合

https://www.fifaindex.com/de/team/1877/BocaJuniors 
BocaJuniors 
https://www.fifaindex.com/de/team/110395/Lan%C3%BAs 
Lan%C3%BAs 
Exception in thread "main" org.jsoup.HttpStatusException: HTTP error 
fetching URL. Status=404, 
URL=https://www.fifaindex.com/de/team/110395/Lan%25C3%25BAs 
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:679) 
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:628) 
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:260) 
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:249) 
at fifa.scraper.Scraper.getTeamStrength(Scraper.java:71) 
at fifa.scraper.Scraper.loadTeams(Scraper.java:60) 
at fifa.scraper.Scraper.main(Scraper.java:23) 

はURLに追加 "25" があります。 System.out.println()でURLを出力すると、URLが機能します。 URLEncoderがなければ最初の行に出力されます:

Exception in thread "main" org.jsoup.HttpStatusException: HTTP error fetching URL. Status=404, URL=https://www.fifaindex.com/de/team/110395/Lan%2525C3%2525BAs/ 

は、どのように私は、UTF-8文字でURLに接続することができますか? ご協力いただきありがとうございます。

答えて

0

あなたのケースでは、サーバーは301のステータスと場所ヘッダーで応答しますが、すでにエンコードされたURLが含まれていますが、Jsoupはもう一度エンコードします。以下のコードは私のために

private static Document sendRequest(String url) { 
     Document doc = null; 
     try { 
      Connection connect = Jsoup.connect(url); 
      connect.request().followRedirects(false); 
      URI u = new URI(url); 
      doc = connect.url(new URI(u.getScheme(), u.getUserInfo(), u.getHost(), u.getPort(), URLDecoder.decode(u.getPath(), "UTF-8"), u.getQuery(), u.getFragment()).toURL()).get(); 
      if (connect.response().statusCode() == 301 && connect.response().header("Location") != null) { 
       return sendRequest(connect.response().header("Location")); 
      } 
      return doc; 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (URISyntaxException e) { 
      e.printStackTrace(); 
     } 
     return doc; 
    } 

    public static void main(String[] args) { 

     String url = null; 
     url = "https://www.fifaindex.com/de/team/110395/Lanús"; 
//  url = "https://www.fifaindex.com/de/team/1877/BocaJuniors"; 
     Document doc = sendRequest(url); 
     Elements strength = doc.getElementsByClass("badge r3"); 
     System.out.println(Integer.parseInt(strength.get(0).text())); 
     System.out.println(Integer.parseInt(strength.get(1).text())); 
     System.out.println(Integer.parseInt(strength.get(2).text())); 
} 
+0

おかげで動作しますが、私はUrlEncoder.encode例外がある取り外すとき:HTTPエラーフェッチURL:スレッド「メイン」org.jsoup.HttpStatusExceptionの例外。ステータス= 404、URL = https://www.fifaindex.com/de/team/110395/Lan%2525C3%2525BAs/余分な「25」の2倍です。 – ToKar

+0

問題が見つかったようです。サーバは301コードとリダイレクトの場所を返すことがあります。この場所は既にエンコードされていますが、JSOUPはもう一度エンコードします。コードスニペットを準備します。 –

+0

私の答えを見てください。コードスニペット付き。 –