2016-09-19 9 views
-1

からすべてのリンクを取得する方法* vedeo.com /ダウンロード?URL = https://www.youtube.com/watch?v=GQx7W3jrZiE私は<a href="https://s">https://s</a>からすべてのリンクを取得したいウェブ

しかし、私は自分のコードを実行したとき、私はちょうど最初の1を取得します。 すべてのリンクを取得するには?ループや何かを使用して、どのようにループを作成する.. 誰も私を助けることができますか?

package com.example.root.mytestjsoup; 

import android.os.AsyncTask; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 

public class MainActivity extends AppCompatActivity { 

    Button but; 
    TextView text; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     but = (Button)findViewById(R.id.button); 
     text = (TextView)findViewById(R.id.textView); 

     but.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       new scrap().execute(); 
      } 
     }); 

    } 

    public class scrap extends AsyncTask<Void,Void,Void>{ 

     String word; 
     Elements link; 
     Element linx; 
     String linkHref; 

     @Override 
     protected Void doInBackground(Void... params) { 

      try { 
       Document doc = Jsoup.connect("https://savedeo.com/download?url=https://www.youtube.com/watch?v=GQx7W3jrZiE").get(); 
       link = doc.select("a[data-event]"); 
       Elements div = doc.getElementsByAttribute("data-event"); 
       String attr = div.attr("href"); // when it change to data-event, it will got the value of data-event. 

       word = attr; 

      }catch (Exception e){ 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void aVoid) { 
      super.onPostExecute(aVoid); 
      text.setText(word); 
     } 
    } 


} 

答えて

0

あなたのコードでは、次の行が無効であると思います。

String attr = div.attr("href"); 

ため

Elements div = doc.getElementsByAttribute("data-event"); 

戻り、エレメントのリストまたは他の言葉で要素

ソリューション

ステップ1:あなたが書くと、この

などの条件をすることができます HREF含まれており、データ・イベントタグを選択意味3210
doc.select("a[href][data-event]") 

ステップ2:あなたはループ要素必要なことができるかどう

Elements links = doc.select("a[href][data-event]"); 
for (Element link : links) { 
    //download from youtube|mp4|1280x720   
    if(link.text().contains("download"))//find the link with some texts 
    { 
     System.out.println("here is the element you need"); 
     System.out.println("\nlink : " + link.attr("href")); 
     System.out.println("text : " + link.text()); 
    } 
} 

参照

https://jsoup.org/cookbook/extracting-data/selector-syntax

https://jsoup.org/cookbook/extracting-data/example-list-links

関連する問題