C# 使用XPath解析网页

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();
        MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据
        Byte[] pageData = MyWebClient.DownloadData(“http://www.163.com”); //从指定网站下载数据
        string pageHtml = Encoding.Default.GetString(pageData);  //如果获取网站页面采用的是GB2312,则使用这句            
        //string pageHtml = Encoding.UTF8.GetString(pageData); //如果获取网站页面采用的是UTF-8,则使用这句
        Console.WriteLine(pageHtml);//在控制台输入获取的内容
        using (StreamWriter sw = new StreamWriter("c:\\test\\ouput.html"))//将获取的内容写入文本
        {
            sw.Write(pageHtml);
        }
        Console.ReadLine();   
    }
    catch(WebException webEx) {
        Console.WriteLine(webEx.Message.ToString());
    }
}

方法二:使用WebBrowser

WebBrowser web = new WebBrowser(); 
web.Navigate("http://www.xjflcp.com/ssc/"); 
web.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(web_DocumentCompleted); 
void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
        { 
            WebBrowser web = (WebBrowser)sender; 
            HtmlElementCollection ElementCollection = web.Document.GetElementsByTagName("Table"); 
            foreach (HtmlElement item in ElementCollection) 
            { 
                 File.AppendAllText("Kaijiang_xj.txt", item.InnerText); 
            } 
        }

方法三:使用HttpWebRequest/HttpWebResponse 

HttpWebRequest httpReq; 
HttpWebResponse httpResp; 
 
string strBuff = ""; 
char[] cbuffer = new char[256]; 
int byteRead = 0; 
string filename = @"c:\log.txt"; 
///定义写入流操作 
public void WriteStream() 
{ 
  Uri httpURL = new Uri(txtURL.Text);
  ///HttpWebRequest类继承于WebRequest,并没有自己的构造函数,需通过WebRequest的Creat方法 建立,并进行强制的类型转换 
   httpReq = (HttpWebRequest)WebRequest.Create(httpURL); 
  ///通过HttpWebRequest的GetResponse()方法建立HttpWebResponse,强制类型转换
   httpResp = (HttpWebResponse) httpReq.GetResponse(); 
  ///GetResponseStream()方法获取HTTP响应的数据流,并尝试取得URL中所指定的网页内容
   ///若成功取得网页的内容,则以System.IO.Stream形式返回,若失败则产生ProtoclViolationException错 误。在此正确的做法应将以下的代码放到一个try块中处理。这里简单处理 
  Stream respStream = httpResp.GetResponseStream();
  ///返回的内容是Stream形式的,所以可以利用StreamReader类获取GetResponseStream的内容,并以
   StreamReader类的Read方法依次读取网页源程序代码每一行的内容,直至行尾(读取的编码格式:UTF8) 
  StreamReader respStreamReader = new StreamReader(respStream,Encoding.UTF8);
  byteRead = respStreamReader.Read(cbuffer,0,256); 
  while (byteRead != 0) 
  { 
    string strResp = new string(cbuffer,0,byteRead); 
       strBuff = strBuff + strResp; 
       byteRead = respStreamReader.Read(cbuffer,0,256); 
  } 
  respStream.Close(); 
  txtHTML.Text = strBuff; 
}

 

posted on 2018-08-13 11:03  梦琪小生  阅读(1402)  评论(0编辑  收藏  举报

导航