private List<TinTuc> GetNews() { HtmlWeb hw = new HtmlWeb(); HtmlAgilityPack.HtmlDocument doc = hw.Load(newsUrl); HtmlNode root = doc.DocumentNode; HtmlNode content = root.SelectSingleNode("//*[@id='category']/div/section/section[2]"); HtmlNodeCollection listNews = content.SelectNodes("article"); List<TinTuc> result = new List<TinTuc>(); foreach (HtmlNode news in listNews) { HtmlNode divImage = news.SelectSingleNode("div[@class='cover']"); string style = divImage.GetAttributeValue("style", ""); string img = GetImage(style); string link = divImage.SelectSingleNode("a").GetAttributeValue("href", "#"); string title = news.SelectSingleNode("header/h1/a").InnerText.Trim(); string time = news.SelectSingleNode("header/time").GetAttributeValue("datetime", ""); string sumary = news.SelectSingleNode("header/p[@class='summary']").InnerText.Trim(); DateTime date = DateTime.Parse(time); TinTuc tt = new TinTuc() { TieuDe = title, Link = newsUrlBase + link, Hinh = img, MoTa = sumary, Ngay = date }; result.Add(tt); } return result; }
public void InsertNews(TinTuc news) { db.TinTucs.Add(news); db.SaveChanges(); }