2016-05-07 13 views
0

私の小さなアプリでは、HTMLをダウンロードするためにフレームワーク "Jsoup"を使用していますが、問題は私のコードがいくつかのURLで動作しないということです。これは私のコードです:なぜJsoupは一部のURLに接続できないのですか?

public static void main(String[] args) { 

    String html=null; 

    //Descargamos el html 
    String url = "http://www.opposingviews.com"; 
    Connection conn = Jsoup.connect(url); 
    try { 
     Response resp = conn.execute(); 
     if (resp.statusCode() != 200) { 
      System.out.println("Error: "+resp.statusCode()); 
     }else{ 
      System.out.println(Thread.currentThread().getName()+" is downloading "+ url); 
      //html = conn.get().html(); 
     } 
    }catch(IOException e) { 
      System.out.println(e.getStackTrace()); 
      System.out.println(Thread.currentThread().getName()+"No puedo conectar con "+ url); 
      System.out.println("No se puede conectar"); 
    } 

などいくつかのURLで動作いけない:

http://www.topix.com 
http://www.wittyfeed.com 
http://www.wittyfeed.com... 

しかし、のような他の人と協力:http://www.google.com, http://www.amazon.es ...

エラーが

org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:590), 
org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:540), 
org.jsoup.helper.HttpConnection.execute(HttpConnection.java:227), 
Practica1.prueba.main(prueba.java:34) 
です

この動作の問題は何ですか?

+0

これらのURLにはどのような問題がありますか? – Arpan

+0

エラー 'stackTrace'を追加してください。@RandallDaniBarrientos –

+0

@VikrantKashyapエラー:[org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection.java:590)、org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection .java:540)、org.jsoup.helper.HttpConnection.execute(HttpConnection.java:227)、Practica1.prueba.main(prueba.java:34)]。 –

答えて

2

まず最初は、あなたがそうのようなユーザーエージェントを追加してください

http://www.topix.comorg.jsoup.HttpStatusException: HTTP error fetching URL. Status=403, URL=http://www.topix.com 

あるURL

に接続しようとすると、あなたは何例外を取得印刷する必要があり、以下の

Connection conn = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); 

がコードに変更されました

import java.io.IOException; 
import org.jsoup.Connection; 
import org.jsoup.Connection.Response; 
import org.jsoup.Jsoup; 


public class JsonExample { 

    public static void main(String[] args) { 

     String html=null; 

     //Descargamos el html 
     String url = "http://www.topix.com"; 
     Connection conn = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); 
     try { 
      Response resp = conn.execute(); 
      if (resp.statusCode() != 200) { 
       System.out.println("Error: "+resp.statusCode()); 
      }else{ 
       System.out.println(Thread.currentThread().getName()+" is downloading "+ url); 
       //html = conn.get().html(); 
      } 
     }catch(IOException e) { 
      System.out.println(e.getStackTrace()); 
      System.out.println(Thread.currentThread().getName()+"No puedo conectar con "+ url + e); 
      System.out.println("No se puede conectar"); 
     } 
    } 
} 
+0

ありがとう@likeToCode、その仕事! jus 1つの質問。私はこのuserAgentを使って、ダウンロード用の5000のURLを持つファイルを持っています(実際にはスレッドを使用しています)。またはUserAgentとURLのjsoup.connectに依存していますか?ありがとう –

+0

ユーザーエージェントを使用すると、すべてのURLで動作するはずです。あなたはそれを試して、問題があれば手を差し伸べて、私の答えも受け入れることができます:-) – likeToCode

0
Elements link = doc.select("a"); 
     System.out.println(link.size()); 
     int c=0; 
     String[] prices = new String[link.size()]; 
     for (int i = 0; i < link.size(); i++) { 
      prices[i] = link.get(i).attr("href"); 
      if(prices[i].contains("https")){ 
       c++; 
       String nurl = prices[i].replace("%2B","+"); 
       String surl = nurl.replace("%3D","="); 
       String urll=prices[i]; 
       System.out.println(prices[i]); 
       URLEncoder.encode(prices[i], "UTF-8"); 
       System.out.println(c+"\t"+surl); 
//    Connection connection2 = Jsoup.connect(surl); 
//    Response doc2=connection2.execute(); 
       Document doc3 = Jsoup.connect(surl).post(); 
       //Document doc3=Jsoup.connect(makeSearch).get(); 
       String blk=doc3.html(); 
+1

あなたの答えを説明してください! – rptwsthi

関連する問題