c#正则匹配指定地址指定div内容

来源:互联网 发布:ubuntu安装非自由软件 编辑:程序博客网 时间:2024/05/17 01:18

 public string html = "";    protected void Page_Load(object sender, EventArgs e)    {        html = GetHtml("http://1680210.com/html/PK10/pk10kai_luzhufxzh.html");        //Regex reg = new Regex(@"(?m)<title[^>]*>(?<title>(?:\w|\W)*?)</title[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase);        Regex reg = new Regex(@"(?m)<div class=""listbox""[^>]*>(?<div>(?:\w|\W)*?)</div[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase);        Match mc = reg.Match(html);        if (mc.Success)        {            html = mc.Groups["div"].Value.Trim();        }    }




//获取页面内容

/// <summary>      /// 获取页面类容      /// </summary>      /// <param name="strUrl"></param>      /// <returns></returns>      public static string GetHtml(string strUrl)    {        string content;        HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(strUrl);        httpRequest.Referer = strUrl;        httpRequest.UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 7\\_1\\_2 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/11D257 MicroMessenger/5.3.1like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4";        httpRequest.Accept = "text/html, application/xhtml+xml, */*";        httpRequest.ContentType = "application/x-www-form-urlencoded";        httpRequest.Method = "GET";        HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();        using (Stream responsestream = httpResponse.GetResponseStream())        {            using (StreamReader sr = new StreamReader(responsestream, System.Text.Encoding.UTF8))            {                content = sr.ReadToEnd();            }        }        return content;    }


原创粉丝点击