注意:当前方法通过【聚合数据】中的今日头条API实现。
var key ="聚合数据获取到的KEY";
var typeCode ="聚合数据中的新闻分类,例如:top代表头条,tiyu代表体育,yule代表娱乐等。";
string url = string.Format("http://v.juhe.cn/toutiao/index?type={0}&key={1}",typeCode,key);
System.Net.WebClient web = new System.Net.WebClient();
var data = web.DownloadData(url);
var dataStr = System.Text.Encoding.UTF8.GetString(data);
MJuHeNewsView result = JsonToObject<MJuHeNewsView>(dataStr);
if (result.reason != "成功的返回")
return;
foreach (var news in result.result.data)
{
var n = (new TBLNews()
{
Uniquekey = news.uniquekey,
TypeCode = item.TypeCode,
Title = news.title,
Date = news.date,
Category = news.category,
Author_name = news.author_name,
Url = news.url,
Thumbnail_Pic_S = news.thumbnail_pic_s,
Thumbnail_Pic_S2 = news.thumbnail_pic_s02,
Thumbnail_Pic_S3 = news.thumbnail_pic_s03,
Status = (int)TBLNews.EnumStatus.No,
Clicks = 0
});
//将数据插入到数据库
DB.TBLNews.Add(n);
}