| | |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.ComponentModel; |
| | | using System.Configuration; |
| | | using System.Data; |
| | | using System.Diagnostics; |
| | | using System.Drawing; |
| | |
| | | using CrawRobot.BLL; |
| | | using CrawRobot.Model; |
| | | using ImgRec; |
| | | using Newtonsoft.Json; |
| | | using Newtonsoft.Json.Linq; |
| | | |
| | | namespace CrawRobot |
| | | { |
| | |
| | | public const int MOUSEEVENTF_ABSOLUTE = 0x8000; |
| | | public static bool bStop = false; |
| | | public static bool bExiting = false; //正在退出 |
| | | public List<string> listWXGZH=new List<string>();//微信公众号 |
| | | public List<string> listWXGZH = new List<string>();//微信公众号 |
| | | public WebBrowser webBrowser = null; |
| | | public int iFileId = 0; |
| | | public int iStoreNum = 0; |
| | |
| | | /// 导入选中数据 |
| | | /// </summary> |
| | | /// <param name="openfile"></param> |
| | | private void ImportandGenerate(OpenFileDialog openfile,string strFilePath="") |
| | | private void ImportandGenerate(OpenFileDialog openfile, string strFilePath = "") |
| | | { |
| | | try |
| | | { |
| | |
| | | { |
| | | if (srcTable.Rows[i][3].ToString().Contains("迁移") == false) |
| | | { |
| | | listWXGZH.Add(srcTable.Rows[i][3].ToString()); |
| | | listWXGZH.Add(srcTable.Rows[i][3].ToString()); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | //bLoadExcelFile = true; |
| | | lblStatus.Text = "导入最新清单数据成功!总计"+listWXGZH.Count+"条"; |
| | | lblStatus.Text = "导入最新清单数据成功!总计" + listWXGZH.Count + "条"; |
| | | } |
| | | else |
| | | { |
| | |
| | | break; |
| | | } |
| | | |
| | | if (j % 15 == 0&& j>0) |
| | | if (j % 15 == 0 && j > 0) |
| | | { |
| | | richTextLog.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 延迟40分钟:" + j + "\r\n"; |
| | | DelayTime(40*60000); |
| | | DelayTime(40 * 60000); |
| | | } |
| | | |
| | | richTextLog.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Debug Index:" + j + "\r\n"; |
| | |
| | | string strURL = HttpUtility.UrlEncode(txtURL.Text, Encoding.UTF8); |
| | | try |
| | | { |
| | | |
| | | |
| | | strURLSearch = |
| | | $"https://weixin.sogou.com/weixin?type=1&s_from=input&query={strURL}&ie=utf8&_sug_=n&_sug_type_="; |
| | | webBrowser1.Navigate(strURLSearch); |
| | |
| | | strResult = webBrowser1.Document.Body.InnerHtml; |
| | | //strResult = HttpApi(strURLSearch, "", "post"); |
| | | } |
| | | catch |
| | | catch |
| | | { |
| | | try |
| | | { |
| | |
| | | } |
| | | else if (strResult.Contains("我们的系统检测到您网络中存在异常访问请求")) |
| | | { |
| | | |
| | | richTextLog.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 未能获取到最新文章:" + |
| | | listWXGZH[j] + ",系统检测到您网络中存在异常访问请求,我们将等待1s\r\n"; |
| | | DelayTime(1000); |
| | | webBrowser1.Navigate(strURLSearch); |
| | | richTextLog.Text += "系统重新访问URL并等待3s:" + strURLSearch+"\r\n"; |
| | | //截取验证码 |
| | | int VerificationCodeLeft = Convert.ToInt32(ConfigurationManager.AppSettings["VerificationCodeLeft"]); |
| | | int VerificationCodeTop = Convert.ToInt32(ConfigurationManager.AppSettings["VerificationCodeTop"]); |
| | | string Base64Pic = ScreenVerifyCode(VerificationCodeLeft, VerificationCodeTop); |
| | | string VerifyCode = ValidateVerifyCode(Base64Pic); |
| | | if (VerifyCode != null) |
| | | { |
| | | //输入验证码 |
| | | int inputLeft = Convert.ToInt32(ConfigurationManager.AppSettings["inputLeft"]); |
| | | int inputTop = Convert.ToInt32(ConfigurationManager.AppSettings["inputTop"]); |
| | | InputTextInfo(VerifyCode, inputLeft, inputTop); |
| | | //点击提交 |
| | | int determineLeft = Convert.ToInt32(ConfigurationManager.AppSettings["determineLeft"]); |
| | | int determineTop = Convert.ToInt32(ConfigurationManager.AppSettings["determineTop"]); |
| | | ClickButtonBy(determineLeft, determineTop); |
| | | } |
| | | richTextLog.Text += "系统重新访问URL并等待3s:" + strURLSearch + "\r\n"; |
| | | DelayTime(6000); |
| | | strResult = webBrowser1.Document.Body.InnerHtml; |
| | | if (strResult.Contains("最近文章:")) |
| | |
| | | else if (strResult.Contains("我们的系统检测到您网络中存在异常访问请求")) |
| | | { |
| | | richTextLog.Text += "系统等待30分钟解锁\r\n"; |
| | | DelayTime(30*60000); |
| | | DelayTime(30 * 60000); |
| | | } |
| | | else |
| | | { |
| | |
| | | { |
| | | if (e.Url.ToString() != webBrowser1.Url.ToString()) |
| | | return; |
| | | if (e.Url.ToString().IndexOf("weixin.sogou.com")>=0) |
| | | if (e.Url.ToString().IndexOf("weixin.sogou.com") >= 0) |
| | | return; |
| | | if (bStop) |
| | | return; |
| | |
| | | DelayTime(30); |
| | | string strResult = Clipboard.GetData("Text").ToString().Replace("\r\n\r\n", "\r\n");//Tel. #: [+]{0,}[(0-9)]{0,}[0-9\- ]{3,} |
| | | |
| | | |
| | | |
| | | //1.过滤冗余字段 |
| | | string[] strFilterWord = { "赞赏二维码", "点个在看你最好看" }; |
| | | for (int i = 0; i < strFilterWord.Length; i++) |
| | |
| | | string strTitleTmp = strResult.Substring(0, strResult.IndexOf(strContentTime)); |
| | | string[] strTitleSplit = Regex.Split(strTitleTmp, @"\r\n", RegexOptions.IgnoreCase); |
| | | |
| | | if(GetRedudentWordFlag(strTitleTmp)) |
| | | if (GetRedudentWordFlag(strTitleTmp)) |
| | | { |
| | | richTextLog.Text += "Store WebBrowser Text Info Skip! strTitleTmp:" + strTitleTmp + "\r\n"; |
| | | } |
| | |
| | | wechatInfo.WechatName = txtURL.Text.Trim(); |
| | | if (!wechatInfoBll.ExistWechatInfo(wechatInfo, false)) |
| | | { |
| | | iStoreNum++; |
| | | iFileId = wechatInfoBll.AddWechatInfo(wechatInfo); |
| | | if (Clipboard.ContainsData(DataFormats.Html) && iFileId>0) |
| | | { |
| | | if(Clipboard.GetData(DataFormats.Html)==null) return; |
| | | //将剪切板中的内容先转为HTML,再转成图片 |
| | | string html = Clipboard.GetData(DataFormats.Html).ToString(); |
| | | //去除HTML文件中的文件源信息部分 |
| | | html = html.Substring(html.IndexOf("<HTML")); |
| | | webBrowser = new WebBrowser(); |
| | | //是否显式滚动条 |
| | | webBrowser.ScrollBarsEnabled = false; |
| | | webBrowser.ScriptErrorsSuppressed = true; |
| | | //加载 html |
| | | webBrowser.DocumentText = html; |
| | | //页面加载完成执行事件 |
| | | webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted); |
| | | } |
| | | iStoreNum++; |
| | | iFileId = wechatInfoBll.AddWechatInfo(wechatInfo); |
| | | if (Clipboard.ContainsData(DataFormats.Html) && iFileId > 0) |
| | | { |
| | | if (Clipboard.GetData(DataFormats.Html) == null) return; |
| | | //将剪切板中的内容先转为HTML,再转成图片 |
| | | string html = Clipboard.GetData(DataFormats.Html).ToString(); |
| | | //去除HTML文件中的文件源信息部分 |
| | | html = html.Substring(html.IndexOf("<HTML")); |
| | | webBrowser = new WebBrowser(); |
| | | //是否显式滚动条 |
| | | webBrowser.ScrollBarsEnabled = false; |
| | | webBrowser.ScriptErrorsSuppressed = true; |
| | | //加载 html |
| | | webBrowser.DocumentText = html; |
| | | //页面加载完成执行事件 |
| | | webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted); |
| | | } |
| | | } |
| | | |
| | | //richTextLog.Text += "Done!"; |
| | |
| | | { |
| | | //获取解析后HTML的大小 |
| | | System.Drawing.Rectangle rectangle = webBrowser.Document.Body.ScrollRectangle; |
| | | int width = rectangle.Width<1000?1000:rectangle.Width; |
| | | int width = rectangle.Width < 1000 ? 1000 : rectangle.Width; |
| | | int height = rectangle.Height; |
| | | |
| | | //设置解析后HTML的可视区域 |
| | |
| | | Directory.CreateDirectory(dir); |
| | | } |
| | | |
| | | filePath = new StringBuilder(dir).Append("\\").Append(DateTime.Now.ToString("yyyy_MM_dd__HH_mm_ss_")).Append(txtURL.Text.Trim()+"FID"+iFileId).Append(".png").ToString(); |
| | | filePath = new StringBuilder(dir).Append("\\").Append(DateTime.Now.ToString("yyyy_MM_dd__HH_mm_ss_")).Append(txtURL.Text.Trim() + "FID" + iFileId).Append(".png").ToString(); |
| | | bitmap.Save(filePath, System.Drawing.Imaging.ImageFormat.Png); |
| | | } |
| | | |
| | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | richTextLog.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 定时器异常错误\r\n"+ex.ToString(); |
| | | richTextLog.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 定时器异常错误\r\n" + ex.ToString(); |
| | | //ResetSystem("定时器异常提前退出!"); |
| | | //MessageBox.Show("定时器异常提前退出!"+ex.ToString()); |
| | | } |
| | |
| | | MessageBox.Show("系统启动异常终止!确认后可以继续抓取!\r\n错误信息:" + exception.ToString()); |
| | | } |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 指定位置截取验证码 |
| | | /// </summary> |
| | | /// <param name="iLeft"></param> |
| | | /// <param name="iTop"></param> |
| | | /// <returns></returns> |
| | | private string ScreenVerifyCode(int iLeft, int iTop) |
| | | { |
| | | SetCursorPos(iLeft, iTop); |
| | | int iWidth = 120;//int iWidth = 92; |
| | | int iHeight = 45;//int iHeight = 36; |
| | | Bitmap myImage = new Bitmap(iWidth, iHeight); |
| | | Graphics gla = Graphics.FromImage(myImage); |
| | | gla.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; |
| | | gla.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; |
| | | gla.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; |
| | | gla.CopyFromScreen(iLeft, iTop, 0, 0, new Size(iWidth, iHeight)); |
| | | myImage.Save(System.AppDomain.CurrentDomain.BaseDirectory + "/yzm.png"); |
| | | //字面是对当前图片进行了二进制转换 |
| | | MemoryStream ms = new MemoryStream(); |
| | | myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); |
| | | byte[] arr = new byte[ms.Length]; |
| | | ms.Position = 0; |
| | | ms.Read(arr, 0, (int)ms.Length); |
| | | ms.Close(); |
| | | string Base64Pic = Convert.ToBase64String(arr); |
| | | return Base64Pic; |
| | | } |
| | | |
| | | |
| | | // <summary> |
| | | /// 指定位置点击按钮 |
| | | /// </summary> |
| | | /// <param name="iLeft"></param> |
| | | /// <param name="iTop"></param> |
| | | private void ClickButtonBy(int iLeft, int iTop, int DelayMS = 1000) |
| | | { |
| | | DelayTime(200); |
| | | SetCursorPos(iLeft, iTop); |
| | | mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); |
| | | DelayTime(DelayMS); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 指定位置输入内容 |
| | | /// </summary> |
| | | /// <param name="TextInfo"></param> |
| | | /// <param name="iLeft"></param> |
| | | /// <param name="iTop"></param> |
| | | private void InputTextInfo(string TextInfo, int iLeft, int iTop, int DelayMS = 600) |
| | | { |
| | | DelayTime(DelayMS); |
| | | Clipboard.SetDataObject(TextInfo); |
| | | SetCursorPos(iLeft, iTop); |
| | | mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); |
| | | DelayTime(50); |
| | | mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); |
| | | DelayTime(30); |
| | | SendKeys.SendWait("{BACKSPACE}"); |
| | | SendKeys.SendWait("{BACKSPACE}"); |
| | | SendKeys.SendWait("{BACKSPACE}"); |
| | | SendKeys.SendWait("{BACKSPACE}"); |
| | | SendKeys.SendWait("{BACKSPACE}"); |
| | | SendKeys.SendWait("{BACKSPACE}"); |
| | | SendKeys.SendWait("{BACKSPACE}"); |
| | | SendKeys.SendWait("{BACKSPACE}"); |
| | | SendKeys.SendWait("{BACKSPACE}"); |
| | | SendKeys.SendWait("{BACKSPACE}"); |
| | | SendKeys.SendWait("{BACKSPACE}"); |
| | | SendKeys.SendWait("{BACKSPACE}"); |
| | | DelayTime(30); |
| | | SendKeys.SendWait("^v"); |
| | | DelayTime(20); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 调用接口识别验证码 |
| | | /// </summary> |
| | | /// <param name="Base64Pic"></param> |
| | | /// <returns></returns> |
| | | private string ValidateVerifyCode(string Base64Pic) |
| | | { |
| | | //网址信息 http://www.ttshitu.com/ |
| | | // 识别 |
| | | // 图鉴平台的账号 |
| | | string UserName = "xl"; |
| | | // 图鉴平台账号对应的密码 |
| | | string Password = "123456!"; |
| | | // 16:汉字 |
| | | // 14:图片旋转 |
| | | // 11:计算题 |
| | | // 7:无感学习 |
| | | // 4:闪动GIF |
| | | // 3:数英混合 |
| | | // 2:纯英文 |
| | | // 1:纯数字. |
| | | string TypeId = "1"; |
| | | // 超时等待,单位:秒 |
| | | string TimeOut = "60"; |
| | | //将base64强转图片 |
| | | string base64 = Base64Pic.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "");//将base64头部信息替换 |
| | | byte[] bytes = Convert.FromBase64String(base64); |
| | | byte[] VImage = bytes; |
| | | |
| | | var param = new Dictionary<object, object>{ |
| | | {"username",UserName}, |
| | | {"password",Password}, |
| | | {"typeid",TypeId}, |
| | | {"timeout",TimeOut} |
| | | }; |
| | | |
| | | string PostResult = TTHttp.Post("http://api.ttshitu.com/create.json", param, VImage); |
| | | |
| | | //json |
| | | string joSuccess = ""; |
| | | string joMessage = ""; |
| | | // |
| | | string joId = ""; |
| | | string joResult = ""; |
| | | JObject jo = null; |
| | | |
| | | try |
| | | { |
| | | jo = (JObject)JsonConvert.DeserializeObject(PostResult); |
| | | // 识别结果 |
| | | joSuccess = jo["success"].ToString().ToLower(); |
| | | // 关联提示 |
| | | joMessage = jo["message"].ToString(); |
| | | } |
| | | catch { } |
| | | //识别成功 |
| | | if (joSuccess == "true") |
| | | { |
| | | //平台返回的图片ID |
| | | joId = jo["data"]["id"].ToString(); |
| | | // 平台返回的识别结果 |
| | | joResult = jo["data"]["result"].ToString(); |
| | | //MessageBox.Show(joResult); |
| | | // 可以按需要继续写入您的业务逻辑 |
| | | if (joResult != null) |
| | | { |
| | | |
| | | } |
| | | } |
| | | else |
| | | { |
| | | // 识别失败 |
| | | //MessageBox.Show("识别失败:" + joMessage); |
| | | } |
| | | |
| | | return joResult; |
| | | } |
| | | } |
| | | } |