1.在NuGet程序包中添加NSoup包
public static void TestNSoup(string urlStr) { WebClient wc = new WebClient(); byte[] htmlByte = wc.DownloadData(urlStr); string html = Encoding.UTF8.GetString(htmlByte); //声明Document对象 Document doc = NSoupClient.Parse(html); //直接通过url来获取Document对象 Document doc1 = NSoupClient.Connect(urlStr).Get(); Elements div = doc1.GetElementById("标签id").GetElementsByTag("标签名如,div"); //通过id获取元素 Element userNames = doc.GetElementById("标签id"); //通过标签名获取元素 Elements p = doc.GetElementsByTag("p"); //通过类样式获取元素 Elements btnNews = doc.GetElementsByClass("btn-news"); //通过属性获取 Elements attrstyle = doc.GetElementsByAttribute("style"); //遍历获取到的标签 foreach (Element item in div) { Console.WriteLine(item.Html()); } }