HtmlAgilityPack is an open-source class library for parsing HTML elements. The biggest feature is that you can use XPath to parse HMTL. If you have used C # To operate XML, you can also use HtmlAgilityPack. The latest version is 1.4.6, as shown below:
Http://htmlagilitypack.codeplex.com/
The following is a simple example to describe the use of HtmlAgilityPack. to simulate logon, except for the name attribute values in the User name text box and Password text box, you also need to know the values of the hidden controls _ VIEWSTATE and _ EVENTVALIDATION on the page, as well as the name attribute of the submit button. The following describes how to use HtmlAgilityPack to obtain this additional value.
1. add reference to HtmlAgilityPack. dll to the project.
2. put several text box controls and a button control on the Aspx page.
3. The button background events are as follows:
btnHtml_Click( sender, EventArgs e) { (tbUrl.Text.Length > 0) { HtmlWeb htmlWeb = HtmlWeb(); HtmlDocument htmlDoc = htmlWeb.Load(.tbUrl.Text); HtmlNode htmlNode = htmlDoc.DocumentNode.SelectSingleNode(""); viewStateValue = htmlNode.Attributes[""].Value; htmlNode = htmlDoc.DocumentNode.SelectSingleNode(""); eventValidation = htmlNode.Attributes[""].Value; htmlNode = htmlDoc.DocumentNode.SelectSingleNode(""); submitName = htmlNode.Attributes[""].Value; tbViewState.Text = viewStateValue; tbEventValidation.Text = eventValidation; tbSubmitName.Text = submitName; } }
4. Take the logon interface of the blog garden as an example. The obtained interface is as follows:
Sample download