Introduction
This article introduces the tool class (htmlagilitypack) for net to process HTML page elements. DLL), the use of a lot of purposes should be such as collection functions, how to deal with the collected HTML string is a headache, it is too troublesome and error-prone to intercept. All the third-party DLL files in this article are used for processing.
Download
: Http://htmlagilitypack.codeplex.com/
Click "Download" to download the file.
Use
1. Add htmlagilitypack. dll reference (reference class using htmlagilitypack ;).
2. Obtain the value based on the input ID in HTML.CodeAs follows:
// Simulate user requests WebClient = New WebClient (); WebClient. Encoding = System. Text. encoding. utf8; String Htmlcontext = WebClient. downloadstring ( " Http: // stone_w.cnblogs.com/ " ); WebClient. Credentials = Credentialcache. defaultcredentials; // Add authorization certificate WebClient. headers. Add ( " User-Agent " , " Microsoft Internet Explorer " ); WebClient. headers. Add ( " Host " , " Www.cnblogs.com " ); // Obtain HTML elements (htmlcontext is an HTML page string) Htmldocument htmldoc = New Htmldocument (); htmldoc. loadhtml (htmlcontext ); // Load HTML pages Htmlnode navnode = htmldoc. getelementbyid ( " ID " ); Response. Write (navnode. attributes [ " Value " ]. Value );
Summary
Htmlagilitypack can query value by ID and obtain a single element node, which is a built-in method of the htmldocument class. You can try to practiceExercise. Thank you ~