JSoup 获取正文,自动识别页面编码Charset

本文介绍了如何利用JSoup库解析HTML页面,自动识别并处理页面的字符集,确保正确提取正文内容。通过示例代码展示了如何获取页面的charset,并使用正则表达式提取关键信息。
摘要由CSDN通过智能技术生成
 
public static String getContent(String url) throws Exception{
		HttpClient hc = new HttpClient();
		HttpMethod hm = new GetMethod(url);
		int statusCode = -1;
		byte[] result = null;
	    statusCode = hc.executeMethod(hm);
	    if(statusCode != HttpStatus.SC_OK)//判断返回
	       return "";
	    if(hm.getResponseBody()!=null){//获取页面数据
	      result = hm.getResponseBody();//hm.getStatusLine()――http状态和请求结果
	    }
	    String charset = JsoupUtils.getCharset(url); //通过jsoup获得页面的charset
		hm.releaseConnection();
		String data = null;
		if(result != null)              
	       data = new String(result,charset);//字符编码设置
		return data;
	}


[代码] 获得字符集

/**
	 * 获得字符集
	 */
	public static String getCharset (String siteurl) throws Exception{
		URL url = new 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值