zoukankan      html  css  js  c++  java
  • 多站点图片爬虫框架

    调用方法:

     protected void Page_Load(object sender, EventArgs e)
        {
            IDatabaseDAO objBook = DAOFactory.CreateBookDAO();

            string sql = string.Format("select top 1 * from BookInfo with(nolock) where Img is null or img=''");
            DataTable dt = objBook.ExecuteDataSetSQL(sql).Tables[0];
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                NewImg ni = new NewImg(ImgSpec.imgWidth, ImgSpec.imgHeight);
                ni.filltype = 0;
                ImgManager im = new ImgManager(ni, dt.Rows[i]["Title"].ToString(), 1);
                string msg = "";
                List<NewImg> lstni = new List<NewImg>();
                lstni = im.getImgList(ref msg);
                if (lstni.Count > 0)
                {
                    NewImg ni2 = lstni[0];
                    ni2.imgid = int.Parse(dt.Rows[i]["bid"].ToString());
                    imgSave(ni2);
                }
                else
                {
                    if (sb.Length > 0) sb.Append("|");
                    sb.Append(HttpUtility.UrlEncode(dt.Rows[i]["Title"].ToString()));
                }
                if (sb.Length > 100)
                {
                    configSrv.configSoapClient csc = new configSrv.configSoapClient();
                    csc.addWordToQueue(sb.ToString(), "Google", 2239);//2239是用来识别哪个站点发起的,可以是任意整数
                    sb = new StringBuilder();
                }
                Thread.Sleep(1000);
            }
            if (sb.Length > 0)
            {
                configSrv.configSoapClient csc = new configSrv.configSoapClient();
                csc.addWordToQueue(sb.ToString(), "Google", 2239);//2239是用来识别哪个站点发起的,可以是任意整数
            }

        }

        private void imgSave(NewImg ni)
        {
            try
            {
                IDatabaseDAO objBook = DAOFactory.CreateBookDAO();
                //string sql = "update BookInfo set Img=@Img,ImgThumb=@ImgThumb where bid=@bid";
                string sql = "update BookInfo set Img=@Img where bid=@bid";
                SqlParameter[] para = {
                                          new SqlParameter("@Img",SqlDbType.VarChar),
                                          new SqlParameter("@ImgThumb",SqlDbType.VarChar),
                                          new SqlParameter("@bid",SqlDbType.Int)
                                          };
                string subPath = "\\Images\\{0}×{1}\\{2}";
                string Img = string.Format(subPath, ni.imgwidth, ni.imghight, ni.imgid + ni.ext);
                string imgPath = Sxmobi.FileHelper.GetMapPath("~") + Img;
                Sxmobi.FileHelper.EnsureDir(Path.GetDirectoryName(imgPath));

                File.Copy(ni.generatePath, imgPath,true);
                File.Delete(ni.generatePath);//删除temp文件

                string ImgThumb = string.Format(subPath, ImgSpec.imgThumbWidth, ImgSpec.imgThumbHeight, ni.imgid + ni.ext);
                ni.imgwidth = ImgSpec.imgThumbWidth;//图片新宽
                ni.imghight = ImgSpec.imgThumbHeight;//图片新高
                ni.generatePath = Sxmobi.FileHelper.GetMapPath("~") + ImgThumb;
                ImgManager.ImgMgrLocal(ni);
                if (File.Exists(ni.srcPath))
                    File.Delete(ni.srcPath);//删除temp文件

                para[0].Value = Img;
                para[1].Value = ImgThumb;
                para[2].Value = ni.imgid;
                objBook.ExecuteNonQuerySQL(sql, para);
            }
            catch (Exception ex)
            {
                Sxmobi.LogHelper.Error(this.GetType().ToString(), ex.Message, ex);
            }
        }

    (以上所涉及的类均可在下面爬虫框架的打包文件中找到)

    爬虫框架下载地址http://download.csdn.net/detail/dasihg/4133312

  • 相关阅读:
    __weak
    c++界面设计皮肤工具
    执行游戏时出现0xc000007b错误的解决方法
    2.4.1-Java语言基础(常量)
    句法模式识别(一)-串文法
    一步一步写算法(之hash表)
    LaTeX新人教程,30分钟从全然陌生到基本入门
    初次当面试官的经历和感触
    Android入门第八篇之GridView(九宫图)
    Android-Cannot merge new index 66195 into a non-jumbo instruction的解决的方法
  • 原文地址:https://www.cnblogs.com/dashi/p/4034717.html
Copyright © 2011-2022 走看看