int maxErrorThreshold = 5; // 最多错误阈值 public override void BeginWork() { base.BeginWork(); // 参数设定 bool DeleteBeginPageIsInt = Int32.TryParse(GetParm("DeleteBeginPage"), out DeleteBeginPage); if (!DeleteBeginPageIsInt) { throw new Exception("参数 DeleteBeginPage 不是数字"); } bool DeleteEndPageIsInt = Int32.TryParse(GetParm("DeleteEndPage"), out DeleteEndPage); if (!DeleteEndPageIsInt) { throw new Exception("参数 DeleteEndPage 不是数字"); } if (DeleteBeginPage <= 0 || DeleteEndPage <= 0 || DeleteBeginPage > DeleteEndPage) { throw new Exception("老铁,你输入的参数是什么鬼?"); } // 获取 Cookie var browserCookieGetter = new CrBrowserCookieGetter(startUrl: "https://qudao.gongxiao.tmall.com/supplier/user/invitation_list.htm", endUrlReg: @"^(https|http)://qudao\.gongxiao\.tmall\.com/supplier/user/invitation_list\.htm", caption: "登录天猫供销平台"); browserCookieGetter.UseInputAutoComplete(@"^https://login\.taobao\.com/member/login\.jhtml", new List <string>() { "#TPL_username_1", "#TPL_password_1" }); browserCookieGetter.BeginWork(); // ... Show Dialog Working cookieStr = browserCookieGetter.GetCookieStr(); if (string.IsNullOrEmpty(cookieStr)) { throw new Exception("Cookie 获取未成功"); } Log("\n"); // 执行撤回操作 int totalPage = DeleteEndPage - DeleteBeginPage + 1; for (int i = DeleteBeginPage; i <= DeleteEndPage; i++) { Log($">> 准备撤回第 {i} 页所有卖家,共 {totalPage} 页,还剩 {totalPage - i} 页"); try { WorkOnPage(i); } catch (Exception e) { if (e.Message == "_END_TASK_") { throw new Exception("撤回失败过多,任务中止执行"); } else { LogError(e.Message); } } Log("\n"); } }
public override void BeginWork() { base.BeginWork(); // 参数设定 PageUrl = GetParm("PageUrl"); // 若使用 new Uri() 会把 urlencode 的参数自动 decode PageType = GetParm("PageType"); ImgType = GetParm("ImgType"); CollType = GetParm("CollType"); // 下载页面 LogInfo("开始下载:" + PageUrl); // 选择页面格式 Dictionary <string, string> headers = new Dictionary <string, string> { }; Encoding encoding = Encoding.GetEncoding("UTF-8"); if (PageType.Equals("Alibaba")) { // 获取 Cookie var browserCookieGetter = new CrBrowserCookieGetter(startUrl: "https://login.1688.com/member/signin.htm?from=sm&Done=" + HttpUtility.UrlEncode(PageUrl), endUrlReg: @"^" + PageUrl.Substring(0, PageUrl.IndexOf("?")), caption: "登录 1688"); browserCookieGetter.UseInputAutoComplete(@"^https://login\.1688.com/member/signin\.htm", new List <string>() { "#TPL_username_1", "#TPL_password_1" }); browserCookieGetter.BeginWork(); // ... Show Dialog Working String alibabaCookieStr = browserCookieGetter.GetCookieStr(); if (string.IsNullOrEmpty(alibabaCookieStr)) { throw new Exception("Cookie 获取未成功"); } encoding = Encoding.GetEncoding("gb2312"); headers.Add("cookie", alibabaCookieStr); } var downloadPage = Utils.GetPageByUrl(PageUrl, headers, null, encoding); if (downloadPage.StatusCode != System.Net.HttpStatusCode.OK) { throw new Exception("下载失败 [" + downloadPage.StatusCode + "] " + downloadPage.StatusDescription); } pageContent = downloadPage.Html; LogSuccess("下载完毕"); pageDom = CQ.CreateDocument(pageContent); // 调用指定方法 this.GetType().GetMethod(PageType + ImgType, BindingFlags.NonPublic | BindingFlags.Instance).Invoke(this, new object[] { }); // 显示&采集 AfterGetImgUrl(); }
int maxErrorThreshold = 5; // 最多错误阈值 public override void BeginWork() { base.BeginWork(); // 参数设定 SellerId = GetParm("SellerId").Trim(); SellerIdArr = SellerId.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); if (SellerIdArr.Length <= 0) { throw new Exception("卖家ID不能一个也没有啊"); } // 获取 Cookie var browserCookieGetter = new CrBrowserCookieGetter(startUrl: "https://qudao.gongxiao.tmall.com/supplier/user/invitation_list.htm", endUrlReg: @"^(https|http)://qudao\.gongxiao\.tmall\.com/supplier/user/invitation_list\.htm", caption: "登录天猫供销平台"); browserCookieGetter.UseInputAutoComplete(@"^https://login\.taobao\.com/member/login\.jhtml", new List <string>() { "#TPL_username_1", "#TPL_password_1" }); browserCookieGetter.BeginWork(); // ... Show Dialog Working cookieStr = browserCookieGetter.GetCookieStr(); if (string.IsNullOrEmpty(cookieStr)) { throw new Exception("Cookie 获取未成功"); } // 获取 _tb_token_ var homePageUrl = "https://qudao.gongxiao.tmall.com/supplier/user/invitation_list.htm"; string homePageHtml = ReqByUrl(homePageUrl); LogSuccess($"{homePageUrl} 下载完毕"); _tb_token_ = new Regex("(?smi)name=(?:\"|')_tb_token_(?:\"|') type=(?:\"|')hidden(?:\"|') value=(?:\"|')(.*?)(?:\"|')").Match(homePageHtml).Groups[1].Value; if (string.IsNullOrEmpty(_tb_token_)) { throw new Exception("_tb_token_ 获取失败"); } LogSuccess($"已获取 _tb_token_ = {_tb_token_}"); Log("\n"); // 执行邀请操作 var index = 0; foreach (var entry in SellerIdArr) { if (maxErrorThreshold != 0 && errorSeller.Count >= maxErrorThreshold) { if (MessageBox.Show($"邀请失败次数已满 {maxErrorThreshold} 次" + Environment.NewLine + "是否中止任务?", this.GetType().ToString(), MessageBoxButtons.YesNo) == DialogResult.Yes) { throw new Exception("邀请失败过多,任务中止执行"); } else { maxErrorThreshold = 0; } } Log($">> 准备邀请第 {index+1} 个卖家,共 {SellerIdArr.Count()} 个,还剩 {SellerIdArr.Count() - (index+1)} 个"); string sellerId = entry.ToString(); try { InviteSellerOnce(sellerId); } catch (Exception e) { LogError(e.Message); errorSeller.Add(sellerId); } Log("\n"); index++; } LogInfo($"共邀请 {SellerIdArr.Count()} 个卖家,成功邀请 {SellerIdArr.Count() - errorSeller.Count} 个,邀请失败 {errorSeller.Count} 个"); }