首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

网页编码有关问题

2012-06-05 
网页编码问题求助网页是:http://news.csdn.net/a/20120528/2806041.htmljava的httpclient抓取来后无论使用

网页编码问题求助
网页是:http://news.csdn.net/a/20120528/2806041.html
java的httpclient抓取来后无论使用什么编码方式,得到的html内容都是乱码。尝试了csdn新闻的多个页面,有的ok有的乱码。

[解决办法]
给你一段代码用来抓取网页格式的! 使用正则表达式可以直接使用

Java code
import java.io.IOException;import  java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import  java.net.*;import java.util.regex.Matcher;import java.util.regex.Pattern;public class network {        public  static   String  getCharset(BufferedReader in){        Pattern  Pname=Pattern.compile("charset=(\\w*)\\p{Punct}*",Pattern.UNIX_LINES);         Matcher  Mk  =null;           String    name=null;                    try{                for(int jk=0 ; jk<4 ; jk++){                      for(int i=0 ; i<4 ; i++)  name=in.readLine();                  Mk   =Pname.matcher(name);                   if(Mk.find()) break;               }             return   Mk.group(1) ;            }catch(IllegalStateException e){                return null;           }catch( IOException e){               return null;            }finally{                  try {                  if(in != null)    in.close();                } catch (IOException e) {                    return null;                }             }                       }       } 

热点排行