您的位置:首页 > 编程语言 > C#

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

2017-08-01 11:43 246 查看
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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: