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に接続することができますか? ご協力いただきありがとうございます。
おかげで動作しますが、私はUrlEncoder.encode例外がある取り外すとき:HTTPエラーフェッチURL:スレッド「メイン」org.jsoup.HttpStatusExceptionの例外。ステータス= 404、URL = https://www.fifaindex.com/de/team/110395/Lan%2525C3%2525BAs/余分な「25」の2倍です。 – ToKar
問題が見つかったようです。サーバは301コードとリダイレクトの場所を返すことがあります。この場所は既にエンコードされていますが、JSOUPはもう一度エンコードします。コードスニペットを準備します。 –
私の答えを見てください。コードスニペット付き。 –