Recently did a project, asked to obtain the major mainstream web page of the key information, I have known the knowledge of web crawler, so think of the web crawler to realize the function
First attempt:
Use WebClient to get the contents of a remote Web page and then filter it with regular expressions
However, because regular expressions for me, writing more complex, study for a half month, little progress is not, every day to look at regular expressions like the heavenly book (back to the people who need to ask a very good)
The first attempt failed, the project will be accepted immediately, this function has been jammed,,,,,,,
Suddenly, on the internet to see someone mentioned htmlagilitypack this open-source toolkit, Ben wanted to try the attitude (because I have no hope for this page parsing)
With just a few lines of code, it was just as fulfilling as my needs to be (using Htmlagilitypack here requires learning a little bit about XPath, but it's simple, it's easier than regular)
Well, nonsense not much to say, on the code
1, to the official Web download a htmlagilitypack package, address: http://htmlagilitypack.codeplex.com/
2. According to the. NET version of your project, select the appropriate version and introduce the project
3, began to write the code
Htmlagilitypack is basically like all classes, directly using the methods and properties inside the line, in particular, can refer to the official website
Gets the page specified content public void gethtml () {String htmlpath = "http://kaijiang.aicai.com/fcssq/"; Create object Htmlagilitypack.htmldocument doc = new htmlagilitypack.htmldocument (); WebClient WebClient = new WebClient (); WebClient. Credentials = credentialcache.defaultcredentials;//Network voucher byte[] pagedata = webclient. Downloaddata (Htmlpath); String pagehtml = Encoding.Default.GetString (pagedata); The default encoding string pagehtml = Encoding.UTF8.GetString (pagedata);//utf-8 encode//Parse Web page content with Htmlagilitypack Loads the HTML doc. Loadhtml (pagehtml); Select the specified element through XPath; XPath reference: http://www.w3school.com.cn/xpath/xpath_syntax.asp htmlagilitypack.htmlnode Htmlnode = Doc. Documentnode.selectsinglenode ("//div[@id = ' Jq_openresult ']"); StringBuilder sb = new StringBuilder (); string s = ""; Htmlagilitypack.htmlnodecollection nodecollection = htmlNode. ChildNodes; for (int i = 0; i < nodecollection. Count; i++) {if (Nodecollection[i]. Innertext.trim ()! = "") {TextBox1.Text + = Nodecollection[i]. InnerText + "-"; }} TextBox1.Text = TextBox1.Text.Substring (0, textbox1.text.length-1); Console.WriteLine (s); }
At this point, htmlagilitypack exactly according to their own requirements to parse out any of the pages you want, is not very magical ~ ~
On C # parsing Web page