码迷,mamicode.com
首页 > 其他好文 > 详细

爬虫软件开发要用到的代码

时间:2015-12-05 20:54:48      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

1.时间戳转为C#格式时间

        /// <summary>
        /// 时间戳转为C#格式时间
        /// </summary>
        /// <param name="timeStamp">Unix时间戳格式</param>
        /// <returns>C#格式时间</returns>
        private DateTime GetTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }

2.获取验证码

 public Image GetImg(string url)
        {
            if (string.IsNullOrWhiteSpace(url))
            {
                url = string.Format("http://ms.baihe.com/checkcode/defaultImageService?0.{0}", DateTime.Now.Ticks);
            }

            cookieCheckCode = "";

            var item = new HttpItem()
            {
                URL = url,
                //Encoding = System.Text.Encoding.GetEncoding("GBK"),
                Method = "get",
                //IsToLower = false,
                Expect100Continue = false,//代理时用这个
                Cookie = _cookie,
                Timeout = 100000,
                ReadWriteTimeout = 30000,
                UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0",
                ContentType = "text/html",
                ResultType = ResultType.Byte
            };
            var result = http.GetHtml(item);

            if (result.Cookie != null)
                cookieCheckCode += result.Cookie.StartsWith("PHPSESSID")
                    ? result.Cookie.Substring(result.Cookie.IndexOf(,) + 1)
                    : result.Cookie;

            return byteArrayToImage(result.ResultByte);
        }


        private Image byteArrayToImage(byte[] Bytes)
        {
            using (var ms = new MemoryStream(Bytes))
            {
                return Bitmap.FromStream(ms, true);
            }
        }

3.正则表使用类

 Regex re = new Regex(@"&uid=(\d+)\"" class=\""yellow\"">(.+)</a>", RegexOptions.None);
            MatchCollection mc = re.Matches(resulthtml.Html);

            var list = new List<User>();
            foreach (Match match in mc)
            {
                var uid = match.Groups[1].Value;
                var nick = match.Groups[2].Value;
                list.Add(new User { uid = uid, nickname = nick });
            }

4.获取时间

        public long GetTimeLikeJS()
        {
            var st = new DateTime(1970, 1, 1);
            var t = (DateTime.Now.ToUniversalTime() - st);
            return (long)t.TotalMilliseconds;
        }

5.另一线程操作主线程的控件

           this.Invoke((Action)delegate()
                    {
                        this.textBox1.Text = "登录成功";
                    });

 

爬虫软件开发要用到的代码

标签:

原文地址:http://www.cnblogs.com/bqh10086/p/5022207.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!