Asp.net| Program | data | database
I used to hear other people do the collection process, today I also small test skill.
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Net;
Using System.Text;
Using System.Text.RegularExpressions;
Using System.IO;
Using System.Data.SqlClient;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Namespace Temp
{
<summary>
Summary description of the Gethtmlsourcefromurl.
</summary>
public class GetHtmlSourceFromUrl:System.Web.UI.Page
{
Private SqlConnection con;
private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
if (! IsPostBack)
{
Inserttodb ();
}
}
private void Inserttodb ()
{
for (int i=1;i<1000;i++)
{
String Html = String. Empty;
String sqltext= "INSERT into Beijingbus (buslinenumber,html) Values (@BLN, @Html)";
String connectionstring=@ "SERVER=.XXXX; User id=xxxx; Pwd=xxxxx;database=map ";
Con =new SqlConnection (ConnectionString);
SqlCommand cmd = new SqlCommand (Sqltext,con);
String url=@ "http://beijing.ibusdb.com/?busline=" +i+ "&s=busline&x=31&y=18"; ^_^, this is to take Beijing's go to the home page, change the parameters, you can take other cities of the
String Content = String. Empty;
HttpWebRequest request = (HttpWebRequest) webrequest.create (URL);
HttpWebResponse response = (HttpWebResponse) request. GetResponse ();
StreamReader stream = new StreamReader (response. GetResponseStream (), encoding.getencoding ("gb2312"));
Content =stream. ReadToEnd ();
Stream. Close ();
Response. Close ();
int start=3487; Go to the ads to take the text
int end= content.length-4222;//Go to the ads to take the text
if (end-start>0)//judgment exists the bus of the No. I Road
{
Html=regex.replace (Content.substring (Start,end-start), @ "<[^>]+>", ""); Detach HTML code
Try
{
Cmd. Parameters.Add ("@BLN", SqlDbType.Int). Value =i;
Cmd. Parameters.Add ("@Html", Sqldbtype.text). Value= Html;
Con. Open ();
Cmd. ExecuteNonQuery ();
}
catch (SqlException err)
{
Response.Write (Err. message);
Con. Close ();
Break
}
Con. Close ();
}
}
}
--Table SQL script:
CREATE TABLE [dbo]. [Beijingbus] (
[ID] [int] IDENTITY (1,1) not NULL,
[Buslinenumber] [INT] Null
[Html] [nvarchar] (max) COLLATE chinese_prc_ci_as NULL
) on [PRIMARY]