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