Java Jsoup从Html中解析歌词

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


import java.io.IOException;
import java.io.UnsupportedEncodingException;


public class Test
{
	private static String songName = "北京北京";


	public static void main(String args[])
	{
		Test getLrc = new Test();
		String lrcUrl = getLrc.getLrcUrl(songName);
		System.out.println(lrcUrl);
		String lrcTxt = getLrc.getLrcText(lrcUrl);
		System.out.println(lrcTxt);
	}


	public String getLrcUrl(String song)
	{
		String lrcUrl = null;


		try
		{
			String songEnc = toBrowserCode(song, "UTF-8");
			System.out.println(songEnc);
			Document doc = Jsoup.connect(
					"http://www.9ilrc.com/search.php?keyword=" + songEnc
							+ "&radio=song").get();


			Elements links = doc.select("a[href]");
			for (Element link : links)
			{
				if (toBrowserCode(trim(link.text(), 15), "UTF-8").equals(
						songEnc))
				{
					lrcUrl = link.attr("abs:href");
					break;
				}
			}
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lrcUrl;
	}


	public String getLrcText(String url)
	{
		String lrcTxt = null;
		try
		{
			Document doc = Jsoup.connect(url).get();
			Elements links = doc.select("[id]");
			for (Element ele : links)
			{
				if (ele.attr("id").equals("txt"))
				{
					lrcTxt = ele.text().replace(" ", "\n");
					break;
				}
			}
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		return lrcTxt;
	}


	private static String trim(String s, int width)
	{
		if (s.length() > width)
			return s.substring(0, width - 1) + ".";
		else
			return s;
	}


	public String toBrowserCode(String word, String encoding)
			throws UnsupportedEncodingException


	{
		byte[] textByte = word.getBytes(encoding);
		StringBuilder strBuilder = new StringBuilder();


		for (int j = 0; j < textByte.length; j++)
		{
			String hexStr = Integer.toHexString(textByte[j] & 0xff);
			strBuilder.append("%" + hexStr.toUpperCase());
		}


		return strBuilder.toString();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Jsoup是一个用于解析HTML文档的开源库。通过使用Jsoup,您可以轻松地从HTML文档提取数据或进行数据操作。以下是使用Java Jsoup解析HTML的基本步骤: 1. 下载Jsoup库:您可以从Jsoup的官方网站(https://jsoup.org/)下载Jsoup库的最新版本。 2. 导入Jsoup库:将下载的Jsoup库的JAR文件导入到您的Java项目。 3. 创建连接:使用Jsoup.connect()方法创建一个Connection对象,将HTML文档的URL作为参数传递给该方法。 4. 获取Document对象:使用Connection对象的get()方法获取一个Document对象,该对象表示整个HTML文档。 5. 使用选择器进行数据提取:使用Jsoup的选择器语法,您可以根据HTML元素的标签、类名、ID等属性来选择和提取数据。 以下是一个基本的Java Jsoup解析HTML的示例代码: ```java import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class HtmlParser { public static void main(String[] args) { try { // 创建连接 Connection connection = Jsoup.connect("http://example.com"); // 获取Document对象 Document document = connection.get(); // 使用选择器提取数据 Elements links = document.select("a[href]"); for (Element link : links) { System.out.println("Link: " + link.attr("href")); System.out.println("Text: " + link.text()); } } catch (IOException e) { e.printStackTrace(); } } } ``` 这个示例代码将从"http://example.com"网页提取所有链接的URL和文本,并打印出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值