从html文件中获取信息

时间:2016-02-04 17:05:51

标签: java

好的,我正在尝试获取数据标题和href,并将它们分配给java中的变量。

<tr class="pl-video yt-uix-tile " data-video-id="MBBWVgE0ewk" data-set-video-id="" data-title="Windows Command Line Tutorial - 1 - Introduction to the Command Prompt"><td class="pl-video-handle "></td><td class="pl-video-index"></td><td class="pl-video-thumbnail"><span class="pl-video-thumb ux-thumb-wrap contains-addto"><a href="/watch?v=MBBWVgE0ewk&amp;index=1&amp;list=PL6gx4Cwl9DGDV6SnbINlVUd0o2xT4JbMu"

1 个答案:

答案 0 :(得分:1)

如果您不介意包含依赖项,那么有一个很好的库可用于jsoup这类事情。

String html = ...
Document doc = Jsoup.parse(html);

Element tr = doc.select("tr").first();
Element link = tr.select("a").first();

String dataTitle = tr.attr("data-title");
String href = link.attr("href");