如何从网络上获取所有链接

时间:2016-09-19 14:38:57

标签: java android-studio

我想从https://s获取所有链接* vedeo.com/download?url = https://www.youtube.com/watch?v=GQx7W3jrZiE

但是,当我运行我的代码时,我只得到第一个。 如何获得所有链接?通过使用循环或任何东西,以及如何创建循环.. 有人可以帮帮我吗?

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);
        }
    }


}

1 个答案:

答案 0 :(得分:0)

我认为您的代码中的关注行无效。

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

因为

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

返回元素列表或换句话说元素 s

的解决方案

第1步:你可以写这样的条件

doc.select("a[href][data-event]")

表示选择包含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