// 這邊用的型別是 AngleSharp 提供的 AngleSharp.Dom.Url var url = new Url("https://jontcont.github.io/"); // 使用 OpenAsync 來打開網頁抓回內容 var doc = await browser.OpenAsync(url); var htmls = doc.QuerySelectorAll("div.recent-post-item");
foreach (var html in htmls) { var link = html.QuerySelector("a")?.GetAttribute("href"); var title = html.QuerySelector("a")?.GetAttribute("title"); var timer = html.QuerySelector("time")?.GetAttribute("datetime"); var text = html.QuerySelector("div.recent-post-info .content")?.TextContent.Substring(0, 50); DateTime dateTime; if (link != null) { link = "https://jontcont.github.io" + link; } if (timer != null) { string format = "yyyy-MM-ddTHH:mm:ss.fffZ"; // 爬蟲-取得的時間格式 dateTime = DateTime.ParseExact(timer, format, CultureInfo.InvariantCulture);
foreach (var html in htmls) { var link = html.QuerySelector("a")?.GetAttribute("href"); var title = html.QuerySelector("a")?.GetAttribute("title"); var timer = html.QuerySelector("time")?.GetAttribute("datetime"); var text = html.QuerySelector("div.recent-post-info .content")?.TextContent.Substring(0, 50); DateTime dateTime; if (link != null) { link = "https://jontcont.github.io" + link; } if (timer != null) { string format = "yyyy-MM-ddTHH:mm:ss.fffZ"; dateTime = DateTime.ParseExact(timer, format, CultureInfo.InvariantCulture); if (dateTime < DateTime.Now.AddHours(-10)) { break; } contentEmbed.Add(new Embed() { title = title, url = link, color = 0x00ff00, fields = new List<Field>(){ new Field(){ name = "內容", value = text + "...", inline = false } }, }); } }