Private void button#click (Object sender, eventargs E)
{
DT. Rows. Clear ();
// The URL to be crawled
String url = "http://list.mp3.baidu.com/topso/mp3topsong.html? Id = 1 # top2 ";
// Obtain the source code of the specified URL
String strwebcontent = getwebcontent (URL );
// richtextbox1.text = strwebcontent;
// extract the Source Code related to the data.
int ibodystart = strwebcontent. indexof (" int istart = strwebcontent. indexof ("song top500", ibodystart);
int itablestart = strwebcontent. indexof (" int itableend = strwebcontent. indexof ("", itablestart);
string strweb = strwebcontent. substring (itablestart, itableend-itablestart + 8);
// Generate htmldocument
Webbrowser Webb = new webbrowser ();
Webb. navigate ("about: blank ");
Htmldocument htmldoc = Webb. Document. opennew (true );
Htmldoc. Write (strweb );
Htmlelementcollection htmltr = htmldoc. getelementsbytagname ("TR ");
// Datarow ROW = NULL;
Foreach (htmlelement TR in htmltr)
{
String Strid = tr. getelementsbytagname ("TD") [0]. innertext;
String [] info = tr. getelementsbytagname ("TD") [1]. innertext. Split ('(');
String strname = "";
String strsinger = "";
If (info. length! = 2)
{
Strname = info [0];
Strsinger = "unknown ";
}
Else
{
Strname = info [0];
Strsinger = info [1]. Replace (")","");
}
Strid = Strid. Replace (".","");
Datarow ROW = DT. newrow ();
Row ["ID"] = Strid;
Row ["voide"] = strname;
Row ["name"] = strsinger;
DT. Rows. Add (ROW );
String strid1 = tr. getelementsbytagname ("TD") [2]. innertext;
String [] info1 = tr. getelementsbytagname ("TD") [3]. innertext. Split ('(');
String strname1 = "";
String strsinger1 = "";
If (info1.length! = 2)
{
Strname1 = info1 [0];
Strsinger1 = "unknown ";
}
Else
{
Strname1 = info1 [0];
Strsinger1 = info1 [1]. Replace (")","");
}
Strid1 = strid1.replace (".","");
Datarow row1 = DT. newrow ();
Row1 ["ID"] = strid1;
Row1 ["voide"] = strname1;
Row1 ["name"] = strsinger1;
DT. Rows. Add (row1 );
// String strid2 = tr. getelementsbytagname ("TD") [3]. innertext;
// String [] info2 = tr. getelementsbytagname ("TD") [4]. innertext. Split ('(');
// String strname2 = "";
// String strsinger2 = "";
// If (info1.length! = 2)
//{
// Strname2 = info1 [0];
// Strsinger2 = "unknown ";
//}
// Else
//{
// Strname2 = info1 [0];
// Strsinger2 = info1 [1]. Replace (")","");
//}
// Strid1 = strid2.replace (".","");
// Datarow row2 = DT. newrow ();
// Row2 ["ID"] = strid2;
// Row2 ["voide"] = strname2;
// Row2 ["name"] = strsinger2;
// DT. Rows. Add (row2 );
}
/// Insert a database
//// Insertdata (DT );
Datagridview1.datasource = DT. defaultview;
}< br> private string getwebcontent (string URL)
{< br> string strresult = "";
try
{
// create an access target
httpwebrequest request = (httpwebrequest) webrequest. create (URL);
// declare an httpwebrequest
request. timeout = 30000;
// set the connection timeout
request. headers. set ("Pragma", "No-Cache");
// Get a response
Httpwebresponse response = (httpwebresponse) request. getresponse ();
// Obtain the data stream
Stream streamreceive = response. getresponsestream ();
// Parse the obtained data stream encoding so that we can read data normally.
Encoding encoding = encoding. getencoding ("gb2312 ");
Streamreader = new streamreader (streamreceive, encoding );
// Read and retrieve the information in the data stream
Strresult = streamreader. readtoend ();
// Close the stream
Streamreader. Close ();
// Disable the network response stream
Response. Close ();
}
Catch
{
MessageBox. Show ("error ");
}
Return strresult;
}
Static datatable dt = new datatable ();
Private void form1_load (Object sender, eventargs E)
{
Datacolumn id = new datacolumn ();
Datacolumn voidename = new datacolumn ();
Datacolumn name = new datacolumn ();
Id. columnname = "ID ";
Voidename. columnname = "voide ";
Name. columnname = "name ";
DT. Columns. Add (ID );
DT. Columns. Add (voidename );
DT. Columns. Add (name );
}
|
10 years summer Youngor men's lapel short-sleeved t-shirt men's cotton T-shirt 3901 beige |
$118.0 |
|
|
oou! Official flagship store! 2 GB cool dog black bone U disk USB flash U-A01-2G |
159.0 RMB |
|