⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jiaoyou.aspx.cs

📁 资源大小: 116.59KB 资源类型: application 自己写个采集器 采集起来更灵活
💻 CS
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class jiaoyou : System.Web.UI.Page
{
    int num = 0;
    SqlConnection con;
    SqlCommand com;
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.ListBox1.Items.Clear();
        ArrayList peoinfo = new ArrayList();
        for (int i = int.Parse(this.TextBox1.Text); i <= int.Parse(this.TextBox2.Text); i++)
        {
            string okurl = this.TextBox3.Text + i.ToString();

            HttpWebRequest adsdf = (HttpWebRequest)WebRequest.Create(okurl);

            adsdf.Timeout = 10000000;

            HttpWebResponse aaaaaaaaa = (HttpWebResponse)adsdf.GetResponse();

            Stream strm = aaaaaaaaa.GetResponseStream();

            StreamReader sr = new StreamReader(strm, Encoding.GetEncoding("utf-8"));

            string dataHTML = sr.ReadToEnd();

            StripHTML(dataHTML,i);

            strm.Close();
        }

        this.Label1.Text = num.ToString();
    }

    public void StripHTML(string strHtml,int he)
    {
        try
        {
            string strOutput = strHtml.Substring(strHtml.IndexOf("profile_memberintrop"));
            strOutput = strOutput.Substring(0, strOutput.IndexOf("最后登录"));
            
            int i = strOutput.IndexOf("src=\"http://photo");
            int j = strOutput.IndexOf("avatar.jpg") - i;
            if (i == -1 || j == -1)
            {
            }
            else
            {
                string photourl = strOutput.Substring(i + 5, j + 5);//照片



                Guid guid = Guid.NewGuid();
                
                WebClient wc = new WebClient();
                wc.DownloadFile(photourl, this.TextBox4.Text+guid.ToString()+".jpg");


                string name = strHtml.Substring(strHtml.IndexOf("http://images.love21cn.com/w3/profile/i/"));
                name = name.Substring(0, name.IndexOf("</strong>"));

                int k = name.IndexOf(">");
                string phoname = name.Substring(k+1).Trim();  //人名

                this.ListBox1.Items.Add(photourl + "        " + phoname+"    "+"  "+he);

                string info = strHtml.Substring(strHtml.IndexOf("<dl class=\"profile_memberintrot\">"));
                info = info.Substring(0, info.IndexOf("信用等级"));
                string useinfo = "";
                Regex regex = new Regex(@"<([\S\s]*?)>");//[^\x00-\xff]
                MatchCollection matches = regex.Matches(info);
                System.Collections.IEnumerator enu = matches.GetEnumerator();
                while (enu.MoveNext() && enu.Current != null)
                {
                    Match match = (Match)(enu.Current);
                    info = info.Remove(info.IndexOf(match.Value),match.Value.Length);  //人员信息
                }
                useinfo = info;


                string testsex = useinfo.Substring(0, useinfo.IndexOf("年龄"));
                string[] usetestsex = testsex.Split(':');
                string sex = usetestsex[1].Trim();  //性别

                string testage = useinfo.Substring(useinfo.IndexOf("年龄"), useinfo.IndexOf("岁") - useinfo.IndexOf("年龄"));
                string[] usetestage = testage.Split(':');
                string age = usetestage[1].Trim();  //年龄

                string testheight = useinfo.Substring(useinfo.IndexOf("身高"), useinfo.IndexOf("厘米") - useinfo.IndexOf("身高"));
                string[] usetestheight = testheight.Split(':');
                string height = usetestheight[1].Trim();  //身高

                string testqua = useinfo.Substring(useinfo.IndexOf("学历"), useinfo.IndexOf("民族") - useinfo.IndexOf("学历"));
                string[] usetestqua = testqua.Split(':');
                string qua = usetestqua[1].Trim();  //学历

                string testnat = useinfo.Substring(useinfo.IndexOf("民族"), useinfo.IndexOf("所在地区") - useinfo.IndexOf("民族"));
                string[] usetestnat = testnat.Split(':');
                string nat = usetestnat[1].Trim();  //民族

                string testjob = useinfo.Substring(useinfo.IndexOf("职业"), useinfo.IndexOf("婚姻") - useinfo.IndexOf("职业"));
                string[] usetestjob = testjob.Split(':');
                string job = usetestjob[1].Trim();  //职业

                string testmarr = useinfo.Substring(useinfo.IndexOf("婚姻"),11);
                string[] usetestmarr = testmarr.Split(':');
                string    marr = usetestmarr[1].Trim();  //婚姻

                string liuyan = strHtml.Substring(strHtml.IndexOf("内心独白"));
                liuyan = liuyan.Substring(0, liuyan.IndexOf("</dl>"));
                string liuyaninfo = "";
                Regex regex1 = new Regex(@"<([\S\s]*?)>");  //[^\x00-\xff]
                MatchCollection matches1 = regex1.Matches(liuyan);
                System.Collections.IEnumerator enu1 = matches1.GetEnumerator();
                while (enu1.MoveNext() && enu1.Current != null)
                {
                    Match match1 = (Match)(enu1.Current);
                    liuyan = liuyan.Remove(liuyan.IndexOf(match1.Value), match1.Value.Length);  //留言
                }
                liuyaninfo = liuyan;

                string endneirong = liuyaninfo.Substring(liuyaninfo.IndexOf("佳缘标签"));

                string testintrod = endneirong.Substring(0,endneirong.IndexOf("心中的"));
                string[] usetestintrod = testintrod.Split(':');
                string introd = "";    //个人简介
                if (usetestintrod[1].IndexOf("佳缘标签") != -1)
                {
                    introd = usetestintrod[1].Trim().Replace("佳缘标签", "个人信息");
                }
                else
                {
                    introd = usetestintrod[1].Trim();
                }

                string testfriend = endneirong.Substring(endneirong.IndexOf("心中的"));
                string[] usetestfriend = testfriend.Split(':');
                string friend = usetestfriend[1].Trim();  //理想对象

                num++;

                string sql = string.Format("insert into love_main (username,password,sex,age,nat,love,height_,edu,field,aboutus,lxdx) values ('{0}','1234567','{1}',{2},'{3}','{4}',{5},'{6}','{7}','{8}','{9}')", phoname,sex,age,nat,marr,height,qua,job,introd,friend);
                insertdata(sql);


            }




        }
        catch
        {
            this.Label1.Text = "第 " + he.ToString() + " 数据出现异常";
        }
    }

    public bool openDB()
    {
        try
        {
            con = new SqlConnection("server=192.168.1.228;database=yzlove;uid=sa;pwd=sa");
            con.Open();
            return true;
        }
        catch
        {
 
        }
        return false;
    }

    public void insertdata(string sql)
    {
        if (openDB())
        {
            com = new SqlCommand();
            com.Connection = con;
            com.CommandText = sql;
            com.ExecuteNonQuery();
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -