Jsoup:如何获取视频网址?

时间:2013-06-14 06:15:10

标签: android url video jsoup html-parser

我正在尝试从html解析视频网址。我正在使用Jsoup解析来解析它。我想解析

<meta property="og:video" content="http://video.foxnews.com/assets/video-player.swf?video_id=2475698001001&d=video.foxnews.com&auto_play=true">
来自this html

。我如何从上面这一行获得这些内容。

现在我的代码是:

try {
            Connection.Response response = Jsoup
                    .connect(htmlUrl)
                    .userAgent(
                            "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
                    .timeout(10000).execute();

            int statusCode = response.statusCode();
            Log.d("TAG", " status code is: " + statusCode);
            if (statusCode == 200) {
                doc = Jsoup.connect(htmlUrl).timeout(1000 * 100).get();
                Log.d("TAG","Document is created : " +  doc );
                Elements element = doc.select("meta");
                for (Element urls : element) {
                    //System.out.println(urls.text());
                    Log.d("TAG", " url is: " + urls.attr("property") + " " + urls.attr("og:video"));
                }
            } else {
                System.out.println("received error code : " + statusCode);
            }

我的代码应该改变什么?

提前谢谢!

2 个答案:

答案 0 :(得分:2)

假设您正在使用asynctask / thread进行网络相关操作。

假设它是第一个元素,你可以按照下面的方式执行

 String myhtml ="<meta property="+"og:video" +" "+"content="+"http://video.foxnews.com/assets/video-player.swf?video_id=2475698001001&d=video.foxnews.com&auto_play=true"+">";
 Document doc = Jsoup.parse(myhtml);  
 Element eMETA = doc.select("meta").first();
 String s = eMETA.attr("content"); 

答案 1 :(得分:0)

我改变了我的代码如下:

try {
            Connection.Response response = Jsoup
                    .connect(htmlUrl)
                    .userAgent(
                            "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
                    .timeout(1000000).execute();

            int statusCode = response.statusCode();
            Log.d("TAG", " status code is: " + statusCode);
            if (statusCode == 200) {
                doc = Jsoup.connect(htmlUrl).timeout(1000 * 1000).get();
                Elements meta = doc.select("meta[property=og:video]");

                for (Element src : meta) {
                    if (src.tagName().equals("meta"))
                        Log.d("TAG", " content: " + src.attr("content"));
                    else
                        Log.d("TAG", src.tagName());
                }
            } else {
                System.out.println("received error code : " + statusCode);
            }
        } catch (IOException e) {
            Log.d("TAG", " Exception " + e);
            e.printStackTrace();

        }