抓取数据时设置抓回来的数据格式,这样就不会乱码了。

  #region 获得指定URL的HTML代码

    /// <summary>

    /// 获得指定URL的HTML代码

    /// </summary>

    /// <param name="_URL"></param>

    /// <returns></returns>

    public static String GetHTMLCode(String _URL)

    {

        string strNew = "";

        try

        {

            System.Net.WebClient client = new WebClient();

            byte[] page = client.DownloadData(_URL);

            strNew = System.Text.Encoding.GetEncoding("utf-8").GetString(page);//设置抓回来的数据格式,以前的方法先得到网页内容(第一步),再读取其中的文本并转换编码((第二步))。现在的方法是:在第一步就转编码了:

        }

        catch

        {

            strNew = "无法连接网页";

        }

 

        return strNew;

    }

    #endregion

posted @ 2015-10-09 21:01  z542601362  阅读(178)  评论(0)    收藏  举报