日期:2014-05-19  浏览次数:20907 次

自动获取新闻是如何实现的?
如题,假如我想使用程序自动获取新华网上的新闻,应该如何实现?
给些思路,十分感谢,给些代码,感激涕零。

------解决方案--------------------
WebClient或者WebRequest

网上例子很多
------解决方案--------------------
1.下载源码
2.正则提取
------解决方案--------------------
关注
------解决方案--------------------
public static string GetHttpSourceValue(string a_strUrl)
{

string strResult;
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(new System.Uri(a_strUrl));
myReq.Method = "GET ";
myReq.Accept = "*/* ";
myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322) ";

try
{
HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();

string tmp = myReq.Headers.ToString();

Stream myStream = HttpWResp.GetResponseStream();

StreamReader sr = new StreamReader(myStream, Encoding.Default);
StringBuilder strBuilder = new StringBuilder();
while (-1 != sr.Peek())
{
strBuilder.Append(sr.ReadLine() + "\r\n ");
}

strResult = strBuilder.ToString();
// StreamWriter sw = new StreamWriter( "E:\\1.txt ", false, Encoding.Default);
// sw.Write(strResult);


myStream.Close();
sr.Close();
// sw.Close();
}
catch (Exception exp)
{
strResult = "错误: " + exp.Message;
}


return strResult;




// StreamWriter sw = new StreamWriter(SaveFileName(), false, Encoding.Default);
// sw.Write(body);
// sw.Close();
}
}

下载部分
------解决方案--------------------
public static int parserHtml(string input,string PName,string Type,string Address,string MyRegex)
{


Regex reg = new Regex(MyRegex, RegexOptions.IgnoreCase | RegexOptions.Compiled);

MatchCollection ms = reg.Matches(input);

System.DateTime t = new DateTime();
t = System.DateTime.Now;



// string temp = " ";


try
{



foreach(Match m in ms)
{

temp += m.Groups[ "type "].Value.Trim()+ " "+m.Groups[ "area "].Value.Trim()+ " "+m.Groups[ "price "].Value.Trim()+ " "+m.Groups[ "num "].Value.Trim()+ " "+m.Groups[ "state "].Value.Trim()+ "\r\n ";
}


}
catch(Exception e)
{
MessageBox.Show(e.ToString());
return null;
}


}