using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Web; using System.Windows.Forms; using CrawRobot.BLL; using CrawRobot.Class; using CrawRobot.Model; using ImgRec; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace CrawRobot { public partial class frmCollectTI : Form { public frmCollectTI() { InitializeComponent(); } #region 全局变量 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern int SetCursorPos(int x, int y); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); public const int MOUSEEVENTF_LEFTDOWN = 0x2; public const int MOUSEEVENTF_LEFTUP = 0x4; public const int MOUSEEVENTF_WHEEL = 0x800; public const int MOUSEEVENTF_ABSOLUTE = 0x8000; public static bool bStop = false; public static bool bExiting = false; //正在退出 public List listWXGZH = new List();//微信公众号 public WebBrowser webBrowser = null; public int iFileId = 0; public int iStoreNum = 0; public int iCount = 0; #endregion // /// 指定位置点击按钮 /// /// /// 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); } /// /// 指定位置输入内容 /// /// /// /// 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); } private void btnStart_Click(object sender, EventArgs e) { //启动 Start(); //try //{ // 解析获取具体链接 // bStop = false; // btnStart.Enabled = false; // btnContinue.Enabled = true; // btnStop.Enabled = true; // iStoreNum = 0; // if (cboxTime.Checked) //定时循环模式 // { // CollectInfobyTime(); // } // else //普通一次性模式 // { // CollectInfoCommon(); // } //} //catch (Exception exception) //{ // ResetSystem("系统启动异常终止!", true); // MessageBox.Show("系统启动异常终止!确认后可以继续抓取!\r\n错误信息:" + exception.ToString()); //} } ////开始搜索 //private void CollectInfoCommon() //{ // if (listWXGZH.Count == 0) // { // listWXGZH.Add(txtURL.Text.Trim()); // string strURLSearch = // $"https://www.ti.com.cn/"; // } // else // { // for (int j = 0; j < listWXGZH.Count; j++) // { // if (bStop) // { // ResetSystem("系统抓取数据强制停止!bStop=True"); // break; // } // string strURLSearch = ""; // string strResult = ""; // string strURL = HttpUtility.UrlEncode(txtURL.Text, Encoding.UTF8); // try // { // strURLSearch = // $"https://www.ti.com.cn/sitesearch/zh-cn/docs/universalsearch.tsp?langPref=zh-CN&searchTerm={strURL}&nr=9#q={strURL}&numberOfResults=25"; // webBrowser1.Navigate(strURLSearch); // DelayTime(10000); // strResult = webBrowser1.Document.Body.InnerHtml; // } // catch // { // try // { // DelayTime(10000); // strURLSearch = // $"https://www.ti.com.cn/sitesearch/zh-cn/docs/universalsearch.tsp?langPref=zh-CN&searchTerm={strURL}&nr=9#q={strURL}&numberOfResults=25"; // //strURLSearch = // // $"https://www.ti.com.cn/?type=1&s_from=input&query={strURL}&ie=utf8&_sug_=n&_sug_type_="; // strResult = HttpApi(strURLSearch, "", "post"); // } // catch (Exception e) // { // richTextLog.Text += "系统出现异常跳过A!Exception:" + e.ToString(); // } // } // if (strResult.Contains("缺货")) // { // DelayTime(5000); // MessageBox.Show("说明没货"); // //MessageBox.Show("正在刷新"); // CollectInfoCommon(); // } // else // { // DelayTime(5000); // MessageBox.Show("说明有货"); // //strResult.Replace("输入数量", "10"); // //MessageBox.Show("ok"); // } // } // } //} private void Start() { ////刷新 //int refreshLeft = Convert.ToInt32(ConfigurationManager.AppSettings["refreshLeft"]); //int refreshTop = Convert.ToInt32(ConfigurationManager.AppSettings["refreshTop"]); //ClickButtonBy(refreshLeft, refreshTop); ////输入产品 //int productLeft = Convert.ToInt32(ConfigurationManager.AppSettings["productLeft"]); //int productTop = Convert.ToInt32(ConfigurationManager.AppSettings["productTop"]); //if (txtURL.Text.Trim() != null) //{ // InputTextInfo(txtURL.Text.Trim(), productLeft, productTop); //} //else //{ // MessageBox.Show("请输入产品"); //} //string strURL = HttpUtility.UrlEncode(txtURL.Text, Encoding.UTF8); //DelayTime(2000); ////点击搜索产品 //int searchproductLeft = Convert.ToInt32(ConfigurationManager.AppSettings["searchproductLeft"]); //int searchproductTop = Convert.ToInt32(ConfigurationManager.AppSettings["searchproductTop"]); //ClickButtonBy(searchproductLeft, searchproductTop); //DelayTime(7000); ////指定位置输入购买数量 //int purchasequantityLeft = Convert.ToInt32(ConfigurationManager.AppSettings["purchasequantityLeft"]); //int purchasequantityTop = Convert.ToInt32(ConfigurationManager.AppSettings["purchasequantityTop"]); //if (txtquantity.Text.Trim() != null) //{ // InputTextInfo(txtquantity.Text.Trim(), purchasequantityLeft, purchasequantityTop); // DelayTime(2000); //} //else //{ // MessageBox.Show("请输入购买产品的数量"); //} ////点击加入购物车 //int shoppingcartLeft = Convert.ToInt32(ConfigurationManager.AppSettings["shoppingcartLeft"]); //int shoppingcartTop = Convert.ToInt32(ConfigurationManager.AppSettings["shoppingcartTop"]); //ClickButtonBy(shoppingcartLeft, shoppingcartTop); //DelayTime(7000); ////点击结算按钮 //int settlementLeft = Convert.ToInt32(ConfigurationManager.AppSettings["settlementLeft"]); //int settlementTop = Convert.ToInt32(ConfigurationManager.AppSettings["settlementTop"]); //ClickButtonBy(settlementLeft, settlementTop); //DelayTime(10000); ////点击购物车里结算按钮 //int settlementtwoLeft = Convert.ToInt32(ConfigurationManager.AppSettings["settlementtwoLeft"]); //int settlementtwoTop = Convert.ToInt32(ConfigurationManager.AppSettings["settlementtwoTop"]); //ClickButtonBy(settlementtwoLeft, settlementtwoTop); //DelayTime(10000); ////点击2次下滑,再点击(1)下一步 int slidedownwardLeft = Convert.ToInt32(ConfigurationManager.AppSettings["slidedownwardLeft"]); int slidedownwardTop = Convert.ToInt32(ConfigurationManager.AppSettings["slidedownwardTop"]); //ClickButtonBy(slidedownwardLeft, slidedownwardTop); //ClickButtonBy(slidedownwardLeft, slidedownwardTop); //DelayTime(2000); //int nextstepLeft = Convert.ToInt32(ConfigurationManager.AppSettings["nextstepLeft"]); //int nextstepTop = Convert.ToInt32(ConfigurationManager.AppSettings["nextstepTop"]); //ClickButtonBy(nextstepLeft, nextstepTop); //DelayTime(15000); ////2.先点击必选框,再一次点击下滑,最后点击(2)下一步 //int mandatoryLeft = Convert.ToInt32(ConfigurationManager.AppSettings["mandatoryLeft"]); //int mandatoryTop = Convert.ToInt32(ConfigurationManager.AppSettings["mandatoryTop"]); //ClickButtonBy(mandatoryLeft, mandatoryTop); //DelayTime(1000); //ClickButtonBy(slidedownwardLeft, slidedownwardTop); //int nextsteptwoLeft = Convert.ToInt32(ConfigurationManager.AppSettings["nextsteptwoLeft"]); //int nextsteptwoTop = Convert.ToInt32(ConfigurationManager.AppSettings["nextsteptwoTop"]); //ClickButtonBy(nextsteptwoLeft, nextsteptwoTop); ////点击确认并继续 //int determineLeft = Convert.ToInt32(ConfigurationManager.AppSettings["determineLeft"]); //int determineTop = Convert.ToInt32(ConfigurationManager.AppSettings["determineTop"]); //ClickButtonBy(determineLeft, determineTop); //DelayTime(20000); ////3.先点击下滑再点击(3)下一步 //ClickButtonBy(slidedownwardLeft, slidedownwardTop); //int nextstepthreeLeft = Convert.ToInt32(ConfigurationManager.AppSettings["nextstepthreeLeft"]); //int nextstepthreeTop = Convert.ToInt32(ConfigurationManager.AppSettings["nextstepthreeTop"]); //ClickButtonBy(nextstepthreeLeft, nextstepthreeTop); //DelayTime(15000); //4.第4步没有 //5.先点击下滑再点击服务条款阅读,再点击接受,点击下滑,最后点击(4)下一步 //ClickButtonBy(slidedownwardLeft, slidedownwardTop); //int serviceLeft = Convert.ToInt32(ConfigurationManager.AppSettings["serviceLeft"]); //int serviceTop = Convert.ToInt32(ConfigurationManager.AppSettings["serviceTop"]); //ClickButtonBy(serviceLeft, serviceTop); //int acceptLeft = Convert.ToInt32(ConfigurationManager.AppSettings["acceptLeft"]); //int acceptTop = Convert.ToInt32(ConfigurationManager.AppSettings["acceptTop"]); //ClickButtonBy(acceptLeft, acceptTop); //DelayTime(1000); //ClickButtonBy(slidedownwardLeft, slidedownwardTop); //int nextstepfourLeft = Convert.ToInt32(ConfigurationManager.AppSettings["nextstepfourLeft"]); //int nextstepfourTop = Convert.ToInt32(ConfigurationManager.AppSettings["nextstepfourTop"]); //ClickButtonBy(nextstepfourLeft, nextstepfourTop); //DelayTime(15000); ////6.先点击下滑,在选择支付宝或者微信支付,最后点击支付 ////下滑 //DelayTime(3000); //ClickButtonBy(slidedownwardLeft, slidedownwardTop); ////选择支付宝付款 //int alipayLeft = Convert.ToInt32(ConfigurationManager.AppSettings["alipayLeft"]); //int alipayTop = Convert.ToInt32(ConfigurationManager.AppSettings["alipayTop"]); //ClickButtonBy(alipayLeft, alipayTop); ////选择微信支付 //int weChatLeft = Convert.ToInt32(ConfigurationManager.AppSettings["weChatLeft"]); //int weChatTop = Convert.ToInt32(ConfigurationManager.AppSettings["weChatTop"]); //ClickButtonBy(weChatLeft, weChatTop); //DelayTime(1000); //ClickButtonBy(slidedownwardLeft, slidedownwardTop); ////点击支付 //int paymentLeft = Convert.ToInt32(ConfigurationManager.AppSettings["paymentLeft"]); //int paymentTop = Convert.ToInt32(ConfigurationManager.AppSettings["paymentTop"]); //ClickButtonBy(paymentLeft, paymentTop); //DelayTime(10000); int iLeft = Convert.ToInt32(ConfigurationManager.AppSettings["iLeft"]); int iTop = Convert.ToInt32(ConfigurationManager.AppSettings["iTop"]); string Base64Pic = ScreenVerifyCode(iLeft, iTop); string Result = GetNumbers.GeneralBasic(Base64Pic); JObject jo = new JObject(); jo = JsonConvert.DeserializeObject(Result); if (jo["words_result"] != null && jo["words_result"].ToString() != "") { JObject words = jo["words_result"][0].Value(); richTextLog.Text += words["words"].ToString(); MessageBox.Show("已成功完成订单,请支付"); } else { MessageBox.Show("订单出现错误!"); } } /// /// 指定位置截取验证码 /// /// /// /// private string ScreenVerifyCode(int iLeft, int iTop) { SetCursorPos(iLeft, iTop); int iWidth = 600;//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 + "/ZFJT.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; } /// /// 调用接口识别验证码 /// /// /// public static string GeneralBasic(string fileName) { try { string token = GetTokenHelper.getAccessToken(); //string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=" + token; string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + token; Encoding encoding = Encoding.Default; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host); request.Method = "post"; request.KeepAlive = true; // 图片的base64编码 string base64 = fileName.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", ""); string str = "image=" + HttpUtility.UrlEncode(base64); byte[] buffer = encoding.GetBytes(str); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string result = reader.ReadToEnd(); return result; } catch (Exception e) { return "Exception"; } } private void btnStop_Click(object sender, EventArgs e) { //ResetSystem("系统自动抓取手动停止!"); } /// /// 延迟函数防止假死 /// /// public static void DelayTime(int mm) { DateTime current = DateTime.Now; while (current.AddMilliseconds(mm) > DateTime.Now) { if (bExiting) break; Application.DoEvents(); } TimeSpan ts = (DateTime.Now - current); if (mm >= 100) { //ManageFile.AppendFile(ManageFile.strScriptDir + "\\Log.txt", // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + " 系统自动等待" + mm.ToString() + "ms(" + // bExiting.ToString() + ") 实际等待" + ts.TotalMilliseconds + "ms"); } return; } } }