1:首先要獲取網頁中的全部信息,有下面3中方法
using System.Net; private void Page_Load(object sender, System.EventArgs e) } private void InitializeComponent() } Microsoft Visual Studio .net 2003 下运行成功。 接上文,以下是普遍采用的 c# 用System.Net 读取网页源代码 的书写方式。 System.Net.HttpWebRequest req; 方法2 在WindowsApplication 用WebBrowser控件实现 读取网页。 private void Form1_Load(object sender, System.EventArgs e) |
{
#region
//删除脚本
Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring,@"<script[^>]*?>.*?</script>","",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
//删除HTML
Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring,@"<(.[^>]*)>","",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring,@"([/r/n])[/s]+","",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring,@"-->","",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring,@"<!--.*","",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
//Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring,@"<A>.*</A>","");
//Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring,@"<[a-zA-Z]*=/.[a-zA-Z]*/?[a-zA-Z]+=/d&/w=%[a-zA-Z]*|[A-Z0-9]","");
Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring,@"&(quot|#34);","/"",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring,@"&(amp|#38);","&",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring,@"&(lt|#60);","<",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring,@"&(gt|#62);",">",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring,@"&(nbsp|#160);"," ",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring,@"&(iexcl|#161);","/xa1",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring,@"&(cent|#162);","/xa2",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring,@"&(pound|#163);","/xa3",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring,@"&(copy|#169);","/xa9",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring, @"&#(/d+);","",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
Htmlstring.Replace("<","");
Htmlstring.Replace(">","");
Htmlstring.Replace("/r/n","");
//Htmlstring=HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
#endregion
return Htmlstring;
}
3:可以提取字符串中的漢字
(1)方法
string str="56756 後即可更換過分 215 後即可更換過分 12131sfagsa";
string ret=System.Text.RegularExpressions.Regex.Replace(str,@"[^(/u4e00-/u9fa5)]","");
(2)方法
private string getTemp(string str)
{
char[] clist= str.ToCharArray();
string tmpstr = "";
int code = 0;
int chfrom = Convert.ToInt32("4e00", 16); //范围(0x4e00~0x9fff)转换成int(chfrom~chend)
int chend = Convert.ToInt32("9fff", 16);
for(int i=0;i<clist.Length;i++)
{
code =Char.ConvertToUtf32(str,i);
if (code >= chfrom && code <= chend)
{
tmpstr += clist[i].ToString();
}
}
return tmpstr;
}
4:可以去掉數字
public static string RemoveNumber(string key)
{
return Regex.Replace(key, @"/d", "");
}
5:可以去掉非數字
public static string RemoveNotNumber(string key)
{
return Regex.Replace(key, @"[^/d]*", "");
}