【问题标题】:Unable to Parse HTML by using JSoup无法使用 JSoup 解析 HTML
【发布时间】:2020-02-29 02:01:30
【问题描述】:

我想从网站获取 HTML 数据以下载 480p 的视频。但它不起作用。我为此目的使用了一个异步类。这是我的 doInBackground 方法:

@Override
        protected Void doInBackground(Void... voids) {
            try {
              //  DOWNLOAD_URL="http://topvideodownloader.com/?url=https%3A%2F%2Fwww.dailymotion.com%2Fvideo%2Fx7nctdj"
                Log.i("DownloadActivity", DOWNLOAD_URL);
                Connection.Response response = Jsoup.connect(DOWNLOAD_URL)
                        .ignoreContentType(true)
                        .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0")
                        .referrer("http://www.google.com")
                        .timeout(12000)
                        .followRedirects(true)
                        .execute();
                Document doc = response.parse();
                Log.i("DownloadActivity",doc.toString());
//                title = doc.select("div.title").text();
//                Log.e("Main", title);
//                String atag= doc.select("a.vd-down btn btn-default btn-download").attr("href");
//                matag = atag;
//                Log.e("Main", matag);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

但是方法正在获取完整的 HTML。它只获取它的前几行。我也尝试使用

Document doc = Jsoup.connect(DOWNLOAD_URL).userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6").get();

但是用这种方法。它甚至没有一行。

我不知道我做错了什么??请帮我。非常感谢

【问题讨论】:

  • 检查下面一个

标签: java android html android-studio jsoup


【解决方案1】:

试试这个

class TestAsync extends AsyncTask<Void, Integer, String> {
    String TAG = getClass().getSimpleName();

    protected void onPreExecute() {
        super.onPreExecute();
        //show progress bar
    }

    protected String doInBackground(Void...arg0) {
          try {
              //  DOWNLOAD_URL="http://topvideodownloader.com/?url=https%3A%2F%2Fwww.dailymotion.com%2Fvideo%2Fx7nctdj"
                 Document doc = Jsoup.connect(DOWN_URL)
                .header("Accept-Encoding", "gzip, deflate")
                .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0")
                .maxBodySize(0)
                .timeout(0)
                .get();

                Log.i("DownloadActivity",doc.toString());
//                title = doc.select("div.title").text();
//                Log.e("Main", title);
//                String atag= doc.select("a.vd-down btn btn-default btn-download").attr("href");
//                matag = atag;
//                Log.e("Main", matag);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "success";

    }

    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if(result.equals("success"))
         //stop progress bar
    }
}

【讨论】:

猜你喜欢
  • 2013-10-25
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-11
  • 1970-01-01
  • 1970-01-01
  • 2014-01-01
相关资源
最近更新 更多