2016-09-20 13 views
1

URLをthisから検索しようとしています。これは私が取得する必要があるテーブルです:Jsoup [Android]を使用してWebページから特定のテーブルを取得する方法

<table id="h2hSum" class="competitionRanking tablesorter"> 
       <thead> 
       <tr> 
       <th align="center">Team</th> 
       <th align="center">Played</th> 
       <th align="center">Win</th> 
       <th align="center">Draw</th> 
       <th align="center">Lose</th> 
       <th align="center">Score</th> 
       <th>Goals Scored</th> 
       <th>Goals Allowed</th> 
       </tr> 
       </thead> 
       <tbody> 
       <tr> 
       <td><a class="teamLink" href="/soccer-statistics/England/Premier-League-2016-2017/team_info_overall/676_Manchester_City_FC">Manchester City</a></td> 
       <td>140</td> 
       <td>47</td> 
       <td>38</td> 
       <td>55</td> 
       <td>188:205</td> 
       <td>1.34</td> 
       <td>1.46</td> 
       </tr> 
       <tr class="odd"> 
       <td><a class="teamLink" href="/soccer-statistics/England/Premier-League-2016-2017/team_info_overall/661_Chelsea_FC">Chelsea</a></td> 
       <td>140</td> 
       <td>55</td> 
       <td>38</td> 
       <td>47</td> 
       <td>205:188</td> 
       <td>1.46</td> 
       <td>1.34</td> 
       </tr> 
       </tbody> 
      </table> 

これは私が試したものです:

private class SimpleTask1 extends AsyncTask<String, String, String> 
{ 
    ProgressDialog loader; 


    @Override 
    protected void onPreExecute() 
    { 
     loader = new ProgressDialog(MainActivity.this, ProgressDialog.STYLE_SPINNER); 
     loader.setMessage("loading engine"); 
     loader.show(); 

    } 

    protected String doInBackground(String... urls) 
    { 
     String result1 = ""; 
     try { 

      Document doc = Jsoup.connect(urls[0]).get(); 
      Element table = doc.select("table[class=competitionRanking tablesorter]").first(); 
      Iterator<Element> ite = table.select("td").iterator(); 

      ite.next(); 
      Log.w("Value 1: ",""+ ite.next().text()); 
      Log.w("Value 2: ",""+ ite.next().text()); 
      Log.w("Value 3: ",""+ ite.next().text()); 
      Log.w("Value 4: ",""+ ite.next().text()); 

     } catch (IOException e) { 

     } 
     return result1; 
    } 

    protected void onPostExecute(String sampleVal) 
    { 
     loader.dismiss(); 
     Log.e("OUTPUT",""+sampleVal); 



    } 




} 

はしかし、これは例外をスローし、私は同様の答えをしようとしたが、テーブルが使用してアクセスされる答えは異なりますクラス名またはtdの幅。 この表のすべての値にアクセスできるようにするにはどうすればよいですか?親切に助けてください。

+0

してください例外のスタックトレースを送信する –

答えて

1

問題

Iterator<Element> ite = table.select("td").iterator();は、彼らがあなたのIPアドレスを保存するように見えるサイトへの最初の訪問の後

NullPointerException

理由をスローし、登録を求めるあなたの活動がボットと似ていたら2回目の訪問。リダイレクト先のランディングページには表が含まれていないため、tablenullであり、nullにはselect(...)を呼び出すことはできません。

ソリューションサービスの

登録し、あなたのコードにログイン手順を挿入したり、登録ページにリダイレクトされている場合は、IPアドレスを切り替えるためにプロキシを使用しています。 IPがブロックされる時間はわかりませんが、vpnと次のコードを使用すると、20回の連続したクエリを実行するのに問題はありませんでした。だから、ユーザーエージェント、クッキー、元のサイトの要求(例えば、ブラウザでの開発ツール/ネットワークツールで監視)に含まれている他のヘッダフィールドを設定してください:

コード

String userAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36"; 
Response res = Jsoup 
     .connect("http://www.soccerpunter.com/soccer-statistics/England/Premier-League-2016-2017/") 
     .followRedirects(true).userAgent(userAgent).referrer("http://www.soccerpunter.com") 
     .method(Method.GET).header("Host", "http://www.soccerpunter.com").execute(); 

Document doc = Jsoup 
     .connect("http://www.soccerpunter.com/soccer-statistics/England/Premier-League-2016-2017/head_to_head_statistics/all/676_Manchester_City_FC/661_Chelsea_FC") 
     .userAgent(userAgent).timeout(10000).header("Host", "http://www.soccerpunter.com") 
     .cookies(res.cookies()) 
     .referrer("http://www.soccerpunter.com/soccer-statistics/England/Premier-League-2016-2017/") 
     .get(); 

Elements td = doc.select("table.competitionRanking.tablesorter").first().select("td"); 
0

はこれを試してみてください:

Document document = Jsoup.parse(s); 
     Element table = document.select("table[class=competitionRanking tablesorter]").first(); 
     for (Element element:table.select("tr")){ 
      for (Element td:element.select("td")){ 
       System.out.println(td.text()); 
      } 
     } 
関連する問題