简体   繁体   中英

Grabbing information from an html file

好的,我试图获取数据标题和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"

If you don't mind including a dependency, there is a good library for this kind of things called 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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM