public partial class caiji : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string HtmlCode = this.GetHtmlCode("http://news.qq.com/a/20091010/001374.htm
");
GetTitleContent getTitleContent = new GetTitleContent();
getTitleContent.Get(HtmlCode);
string title = getTitleContent.GetTitle();
Literal1.Text = title;
string content = getTitleContent.GetContent();
Literal2.Text = content;
this.SaveNews(title, content);
}
//把新闻标题和新闻内容存入数据库
protected void SaveNews(string title, string content)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=PC-200910081127;database=test;uid=sis;pwd=123";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = string.Format("insert into test values('{0}','{1}')", title, content);
cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
Response.Write("存入数据库成功");
}
//取得网页的源代码
protected string GetHtmlCode(string url)
{
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse WebRes = (HttpWebResponse)WebReq.GetResponse();
Stream StreamRes = WebRes.GetResponseStream();
StreamReader SR = new StreamReader(StreamRes, Encoding.GetEncoding("GB2312"));
string HtmlCode = SR.ReadToEnd();
return HtmlCode;
}
}
//从源代码中取得新闻标题和内容
public class GetTitleContent
{
string title;
string content;
public void Get(string HtmlCode)
{
//获取标题
string pattern = "<div id=\"ArticleTit\">.*</div>";
Match mat = Regex.Match(HtmlCode, pattern, RegexOptions.IgnoreCase);
title = mat.Groups[0].Value.ToString();
//获取内容
pattern = "<div id=\"ArticleCnt\">.*</div>";
mat = Regex.Match(HtmlCode, pattern, RegexOptions.IgnoreCase);
content = mat.Groups[0].Value.ToString();
}
public string GetTitle()
{
return title;
}
public string GetContent()
{
return content;
}
}