. NET crawl Web page data

Source: Internet
Author: User

1, want to get the data of a page through the code, first, according to the right-click View page source code, through analysis. Then through the following code, modify, step by step to find out the required content, stored in the database.

 //get the HTML source of the webpage according to the URL address        Private stringGetwebcontent (stringUrl) {            stringStrresult =""; Try{HttpWebRequest Request=(HttpWebRequest) webrequest.create (URL); //declaring a HttpWebRequest requestRequest. Timeout =30000; //Setting the connection time-out periodRequest. Headers.set ("Pragma","No-cache"); HttpWebResponse Response=(HttpWebResponse) request.                GetResponse (); Stream streamreceive=Response.                GetResponseStream (); Encoding Encoding= Encoding.GetEncoding ("Utf-8"); StreamReader StreamReader=NewStreamReader (streamreceive, encoding); Strresult=Streamreader.readtoend (); }            Catch{MessageBox.Show ("Error"); }            returnstrresult; }        Private voidButton1_Click (Objectsender, EventArgs e) {            //the URL address to crawl            stringURL ="http://kxt.com/data/20.html";//☆☆☆☆☆☆☆☆☆☆//get the source code for the specified URL            stringStrwebcontent =getwebcontent (URL); richTextBox1.Text=strwebcontent; //Take out the source code related to the data            intIbodystart = Strwebcontent.indexof ("<body",0); intIStart = Strwebcontent.indexof ("Historical Data", Ibodystart); intItablestart = Strwebcontent.indexof ("<ul", IStart); intItableend = Strwebcontent.indexof ("</ul>", Itablestart); stringStrweb = strwebcontent.substring (Itablestart, Itableend-itablestart +5); //Generate HTMLDocumentWebBrowser Webb =NewWebBrowser (); Webb. Navigate ("About:blank"); HTMLDocument Htmldoc= Webb. Document.opennew (true); Htmldoc.            Write (Strweb); HtmlElementCollection htmltr= Htmldoc. getElementsByTagName ("Li"); inti =0; foreach(HtmlElement TRinchhtmltr) {i++; if(i = =1 )                {                    Continue; }                if(i = = Htmltr.count-2)                {                     Break; } htmlelementcollection spans= tr. getElementsByTagName ("span"); stringDateTime = spans[0].                                InnerText; stringNetweightounce = spans[1].                InnerText; stringNetweightton = spans[2].                InnerText; stringTotalvalue = spans[3].                InnerText; stringregulation = spans[4].                InnerText; //string affectoil = Spans[5].                                InnerText; //Id, UpdateTime, Netweightounce, Netweightton, Totalvalue, Regulation, FinancetimeSQL Server ado=NewSQL Server (); Ado. AddField ("UpdateTime", DateTime.Now); Ado. AddField ("netweightounce", netweightounce); Ado. AddField ("Netweightton", Netweightton); Ado. AddField ("Totalvalue", Totalvalue); //ADO. AddField ("Effectoil", affectoil);Ado. AddField ("Regulation", regulation); Ado. AddField ("Financetime", Convert.todatetime (DateTime). ToString ("YYYY-MM-DD"));//☆☆☆☆☆☆☆☆☆☆ADO. Insert ("Silver");//☆☆☆☆☆☆☆☆☆☆} MessageBox.Show ("OK"); } 

. NET crawl Web page data

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.