Htmlagilitypack C # Crawler

Source: Internet
Author: User

Main program
classProgram {Static voidMain (string[] args) {            varTMPSTR1 ="http://www.****.com/txtxz/{0}/down.html"; WebClient WC=NewWebClient ();  for(inti =54422; I <54423; i++)            {                varstr =String.Format (TMPSTR1, i); String result=WC.                Downloadstring (str); Bookdataparser Parser=Newbookdataparser (result, str); varitem =parser.                Getbookdata (); if(Item = =NULL) {Console.WriteLine ("fail to get in url{0}", i); Continue; } WC. DownloadFile (item. Downlink, item. Title+". txt");            Console.WriteLine (item); }Console.readkey (); }    }
BookDataParser.cs
/// <summary>    ///BookData parsing/// </summary>     Public classBookdataparser {PrivateString _content;//HTML content        PrivateHTMLDocument _doc;//Htmlagilitypack working with document classes        PrivateString _url;//current link, only for construction bookdata        /// <summary>        ///Construction Bookdataparser/// </summary>        /// <param name= "Content" >HTML content</param>        /// <param name= "url" >Current Link</param>         PublicBookdataparser (stringContentstringURL) {_url=URL; _content=content; _doc=NewHTMLDocument (); _doc.        loadhtml (content); }        /// <summary>        ///Parse return bookdata/// </summary>        /// <returns></returns>         Publicbookdata Getbookdata () {if(! Vaildcontent ()) {return NULL; } varBookData =NewBookData (); //Title            varTitlenode = _doc. Documentnode.selectsinglenode ("//div[@id = ' titlename ']/h1"); varstr = titleNode.ChildNodes.First (n = n.name = ="#text").            InnerHtml; Bookdata.title= str. Substring (0Str. IndexOf ("TXT")); varInfonode = _doc. Documentnode.selectnodes ("//div[@class = ' txt_info ']/span"); //Author            varSTR2 = infonode[0].            InnerText; Bookdata.authorname= str2. Substring (str2. IndexOf (':')+1); //class            varSTR3 = infonode[1].            InnerText; Bookdata.class= Str3. Substring (STR3. IndexOf (":") +1); //Date            varSTR4 =Infonode.last ().            InnerText; Bookdata.uploadtime= DateTime.Parse (STR4. Substring (STR4. IndexOf (":") +1)); //Description            varDescnode = _doc. Documentnode.selectsinglenode ("//div[@class = ' infos_txt ')"); Bookdata.description=GetDesc (Descnode); //downlink            varLinkNode1 = _doc. Documentnode.selectsinglenode ("//div[@class = ' Pan_url ']/a[last ()]"); Bookdata.downlink= Linknode1.getattributevalue ("href",NULL); //InfoLinkBookdata.infolink =_url; returnBookData; }        /// <summary>        ///handling Descnode getting a description/// </summary>        /// <param name= "Descnode" >node that describes the information</param>        /// <returns>Book description</returns>        Private stringGetDesc (Htmlnode descnode) {StringBuilder sb=NewStringBuilder (); foreach(varNodeinchdescnode.childnodes) {if(node. name=="#text")                {                    varstr =node.                    InnerText; if(!string.isnullorwhitespace (str)) {sb. Append (Httputility.htmldecode (str).                    Trim ()); }                }                if(node. name=="BR") {sb. Append ("\ n"); }            }            returnsb.        ToString (); }        /// <summary>        ///Verify that content is empty or illegal/// </summary>        /// <returns></returns>         Public BOOLvaildcontent () {if(_content = =NULL)return false; varnode = _doc. Documentnode.selectsinglenode ("//div[class= ' Blocktitle ']"); returnnode = =NULL|| Node. Innertext.startswith ("error occurred"); }    }

Htmlagilitypack C # Crawler

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.