【Jsoup】抓取动态网页内容
效果展示

1、获取排行榜列表
public static Map<String, String> getTopList(String url) throws IOException { if (StringUtil.isEmpty(url)) { return null; } Document document = getDocument(url); Elements elements = getElements(document, "a"); Map<String, String> items = null; if (elements != null && elements.size() > 0) { items = new HashMap<>(); for (int i = 0; i < elements.size(); i++) { Element element = elements.get(i); String href = element.attr("href").trim(); String txt = element.text().trim(); if (StringUtil.isNotEmpty(href) && StringUtil.isNotEmpty(txt) && href.contains(url.substring(url.lastIndexOf("/")))) { items.put(txt, href.substring(href.lastIndexOf("?"))); } } } return items; } public static Elements getElements(Document doc, String tag) { return doc != null ? doc.select(tag) : null; } public static Document getDocument(String url) throws IOException { if (StringUtil.isEmpty(url)) { return null; } return Jsoup.connect(url).get(); }
2、获取歌曲列表
public static JSONArray getData(String url) throws IOException { if (StringUtil.isNotEmpty(url)) { Document document = getDocument(url); if (document != null) { String dataTxt = document.select("#song-list-pre-data").text(); return JSONArray.parseArray(dataTxt); } } return null; } public Vector<Vector<String>> getSongTableBody(JSONArray array) { if (array == null || array.size() == 0) { return null; } Vector<Vector<String>> data = new Vector<>(); for (Object obj : array) { JSONObject json = (JSONObject) obj; // 歌名 String name = json.getString("name"); // 歌手 String singer = getSingers(json); // 歌曲时长 String durationStamp = json.getString("duration"); // 歌曲时长(格式化后) String duration = getDuration(Long.valueOf(durationStamp)); JSONObject albumJson = json.getJSONObject("album"); // 歌曲ID String songId = json.getString("id"); // 唱片名称 String album = albumJson.getString("name"); // 唱片封面 String albumPic = albumJson.getString("picUrl"); Vector<String> row = new Vector<>(); // 用于展示 row.add(name); row.add(singer); row.add(duration); row.add(album); // 不用于展示 row.add(songId); row.add(albumPic); row.add(durationStamp); data.add(row); } return data; }
3、歌词处理
/** * 获取歌词内容 * * @param songId 歌曲ID * @return 歌词 */ public String lyrics(String songId) { String link = String.format(Application.LYRIC_LINK, songId); try { Document doc = Jsoup.connect(link).get(); if (doc != null) { // 获取文档内容 String txt = doc.select("body").text(); // 解析文档内容,获取歌词信息 JSONObject lyricsJson = JSONObject.parseObject(txt); if (lyricsJson.getInteger("code") == 200) { JSONObject lrc = lyricsJson.getJSONObject("lrc"); return lrc.getString("lyric"); } } } catch (IOException e) { System.err.printf("获取歌曲[%s]歌词文档解析失败:%s", songId, e.getMessage()); } return null; } /** * 获取歌词处理结果 * * @param lyrics 原始歌词 * @return 处理结果 */ public Vector<Vector<String>> getData(String lyrics) { Vector<Vector<String>> data = new Vector<>(); StringReader sr = null; BufferedReader br = null; String line; try { sr = new StringReader(lyrics); br = new BufferedReader(sr); while ((line = br.readLine()) != null) { Vector<String> row = new Vector<>(); String dLine = line.trim(); if (dLine.startsWith("[")) { // 时间 String time = dLine.substring(dLine.indexOf("[") + 1, dLine.indexOf("]")); // 歌词 String lyric = dLine.substring(dLine.indexOf("]") + 1); row.add(time); row.add(lyric); } else { row.add(""); row.add(dLine); } data.add(row); } } catch (IOException e) { System.err.printf("歌词处理失败:%s", e.getMessage()); } finally { try { if (br != null) { br.close(); } if (sr != null) { sr.close(); } } catch (IOException e) { System.err.printf("I/O关闭失败:%s", e.getMessage()); } return data; } } /** * 显示歌词 * * @param songId 歌曲ID * @param table 歌词显示列表 */ public void showLyric(String songId, JTable table) { String lyrics = lyrics(songId); Vector<Vector<String>> data = getData(lyrics); if (data == null || data.size() == 0) { table.setModel(new DefaultTableModel()); return; } Vector<String> header = new Vector<>(); header.add("time"); header.add("lyric"); // 单元格不可编辑 DefaultTableModel model = new DefaultTableModel(data, header) { public boolean isCellEditable(int row, int col) { return false; } }; table.setModel(model); // 首行默认选择 table.changeSelection(0, 0, false, false); // 不显示网格 table.setShowGrid(false); // 首列不显示 TableColumn column = table.getColumnModel().getColumn(0); column.setMinWidth(0); column.setMaxWidth(0); // 隐藏表头 table.getTableHeader().setVisible(false); // 设置行高 table.setRowHeight(24); // 设置单选 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);// 设置渲染器 DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(); // 文本居中显示 renderer.setHorizontalAlignment(SwingConstants.CENTER); table.setDefaultRenderer(Object.class, renderer); }
Ps:
1.获取的音乐信息为JSON格式,需要自行处理获取需要的信息。
2.如果遇到韩文可能乱码,可将需要显示文本的组件字体设置成:Arial Unicode MS。
声明:该文章仅供学习交流使用,不可用于其他途径。如果文章中有侵害到您或贵公司权益的,请联系本人进行删除。

浙公网安备 33010602011771号