Asp. NET: Write a program to steal the National Bus database

Source: Internet
Author: User
Tags empty
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]



Related Article

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.