本文主要向大家介绍了C#编程之C# 使用XPath解析网页,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
1、需要安装库HtmlAgilityPack ,官网http://htmlagilitypack.codeplex.com/
// From File
var doc = new HtmlDocument();
doc.Load(filePath);
// From String
var doc = new HtmlDocument();
doc.LoadHtml(html);
// From Web
var url = "http://html-agility-pack.net/";
var web = new HtmlWeb();
var doc = web.Load(url);
//XPath
var nodes = doc.DocumentNode.SelectNodes("//*[@id=\"body\"]");
XPath语法:http://www.w3school.com.cn/xpath/xpath_syntax.asp
其他解析网页的方法:(引用至:https://blog.csdn.net/shenmegui_zyf/article/details/78784464)
方法一:使用WebClient
static void Main(string[] args)
{
try {
WebClient MyWebClient = new WebClient();