I used to often hear other people doing collection procedures, so today I'm going to try my hand at it. .
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 GetHtmlSourceFromUrl.
/// </summary>
public class GetHtmlSourceFromUrl : System.Web.UI.Page
{
private SqlConnection con;
private void Page_Load(object sender, System.EventArgs e)
{
//Put user code here to initialize the 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 and go to the homepage, change the parameters, You can take other cities
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 advertisement to get the text
int end= Content.Length-4222;//Go to the advertisement to get the text
if(end-start>0) //Judge the existence of the i-th bus
{
Html=Regex.Replace(Content.Substring(start,end-start),@"<[^>]+>",""); //Separate 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]
does not use signatures: http://hi.baidu.com/Chen Li/blog