上个星期开会说现在中国的垃圾邮件很赚钱啊。我也很想多赚点钱。不过做技术的都很死板。脑壳不灵活。只能多研究研究一些方法来实现这样子那样的程序代码,搜集整理的两天。终于找到自己想要的代码。不很完善多多改进就好了。
using System.IO;
using System.Text.RegularExpressions;
public string GetPageContent(string url)
{
//抓取网页源代码
string ContentHtml=String.Empty;
HttpWebRequest rt=null;
HttpWebResponse rs=null;
Stream stream=null;
StreamReader sr=null;
rt=(HttpWebRequest)WebRequest.Create(url);
rs=(HttpWebResponse)rt.GetResponse();
stream=rs.GetResponseStream();
sr=new StreamReader(stream,System.Text.Encoding.Default);
ContentHtml=sr.ReadToEnd();
sr.Close();
stream.Close();
rs.Close();
return ContentHtml;
}
public string SetHttpUrl(string StrText)
{
//用正则表达式识别URL超链接
Regex UrlRegex = new Regex(@"(http:////([/w.]+//?)/S*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
//进规则查询Url
MatchCollection matches = UrlRegex.Matches(StrText);
foreach (Match match in matches)
{
StrText = StrText.Replace(match.Value, string.Format("<a href=/"{0}/" target=/"_blank/">{1}</a>", match.Value, match.Value));
}
return StrText;
}
public string SetEmailUrl(string StrText)
{
//用正则表达式识别Email地址
Regex EmailRegex = new Regex(@"([a-zA-Z_0-9.-]+/@[a-zA-Z_0-9.-]+/./w+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
MatchCollection matches = EmailRegex.Matches(StrText);
foreach (Match match in matches)
{
StrText = StrText.Replace(match.Value, string.Format("<a href=mailto:{0}>{1}</a>", match.Value, match.Value));
}
return StrText;
}
public string GetHttpUrl(string StrText)
{
//将读取出来的全部URL写如文本文件
string strPageUrlFileName=Application.StartupPath+"//HttpPageUrl.txt";
StreamWriter strwriterobj=File.CreateText(strPageUrlFileName);
//用正则表达式识别URL超链接进规则查询Url
Regex UrlRegex = new Regex(@"(http:////([/w.]+//?)/S*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
MatchCollection matches = UrlRegex.Matches(StrText);
foreach (Match match in matches)
{
lstHttp.Items.Add(match.Value.ToString());
strwriterobj.WriteLine(match.Value.ToString());
}
strwriterobj.Close();
return StrText;
}
public string GetEmailUrl(string StrText)
{
//将读取出来的全部URL写如文本文件
string strPageEmailFileName=Application.StartupPath+"//HttpPageEmail.txt";
StreamWriter strwriterobj=File.CreateText(strPageEmailFileName);
//用正则表达式识别Email地址
Regex EmailRegex = new Regex(@"([a-zA-Z_0-9.-]+/@[a-zA-Z_0-9.-]+/./w+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
MatchCollection matches = EmailRegex.Matches(StrText);
foreach (Match match in matches)
{
lstEmail.Items.Add(match.Value.ToString());
strwriterobj.WriteLine(match.Value.ToString());
}
strwriterobj.Close();
return StrText;
}
批量抓取网页代码中的HTTP和邮件地址,IO,正则表达式,抓网页源码