无法从HTML字符串fomat中读取Src属性

时间:2017-07-19 09:50:12

标签: java jsoup

我有一个字符串,如下所示

<iframe class = 'sproutvideo-player' src = '//videos.sproutvideo.com/embed/1c9adbb11d18e3c994/a5fd279ead76557c' width = '630' height = '354' frameborder = '0' allowfullscreen > < /iframe>

我正在尝试从中读取属性src。

我试过如下

String html = "<iframe class = 'sproutvideo-player' src = '//videos.sproutvideo.com/embed/1c9adbb11d18e3c994/a5fd279ead76557c' width = '630' height = '354' frameborder = '0' allowfullscreen > < /iframe>";

    Document doc = Jsoup.parse(html);

    Elements links = doc.select("src");

         System.out.println(links.attr("src"));

你能告诉我如何阅读src属性吗?

2 个答案:

答案 0 :(得分:1)

下面:

 String html = "<iframe class = 'sproutvideo-player' src = '//videos.sproutvideo.com/embed/1c9adbb11d18e3c994/a5fd279ead76557c' width = '630' height = '354' frameborder = '0' allowfullscreen > < /iframe>";

 Document doc = Jsoup.parse(html);
 Element link = doc.select("iframe").first();

 String linkHref = link.attr("src");

请记住,select方法返回参数中所有匹配元素的列表。为此,我在这里使用了.first()

答案 1 :(得分:1)

更改

 Elements links = doc.select("src");

 Elements links = doc.select("iframe");