private bool DoPCRule(NaverVistiRule rule) { try { //访问主页 if (!VisitHomePage(rule)) { return(false); } //TODO:随机前置访问 //输入关键词并点击搜索按钮 if (InputSearchKeyAndClick(rule)) { return(false); } //访问更多商品链接 if (!VisitMoreProducts(rule)) { return(false); } return(true); } catch (Exception ex) { Serilog.Log.Error(ex, "DoRule error"); return(false); } }
private bool InputSearchKeyAndClick(NaverVistiRule rule) { //输入关键词 if (!_webdriverAction.SendKeysById(rule.SearchKeys, "query", _webDriver, rule.WaitSecsInSearch)) { Serilog.Log.Warning("input search key failed"); return(false); } else { Serilog.Log.Information("input search key success"); } //点击搜索按钮 if (!_webdriverAction.DoClickById("search_btn", _webDriver)) { Serilog.Log.Warning("click search button failed"); return(false); } else { Serilog.Log.Warning("click search button success"); return(true); } }
private bool VisitMoreProducts(NaverVistiRule rule) { //获取页面内容 var respage = _webdriverAction.GetPage(_webDriver); if (string.IsNullOrEmpty(respage)) { return(false); } //查找更多产品按钮 IWebElement sectionMore = null; if (respage.Contains("쇼핑 더보기")) { sectionMore = _webdriverAction.GetElementByXpath("//a[text()='쇼핑 더보기']", _webDriver); if (sectionMore == null) { Serilog.Log.Warning("find more product button failed"); return(false); } else { Serilog.Log.Information("find more product button success"); } } else { Serilog.Log.Warning("dom content can not find more product button trace"); return(false); } //提取入口URL地址 var sectionMoreUrl = sectionMore.GetAttribute("href"); Serilog.Log.Information("more product url:{0}", sectionMoreUrl); //定位到元素位置 if (!_webdriverAction.ScrollToElement(sectionMore, _webDriver)) { return(false); } //访问更多商品地址 if (_webdriverAction.GotoUrl(sectionMoreUrl, _webDriver)) { Serilog.Log.Warning("visit more product url failed"); return(false); } { Serilog.Log.Information("visit more product url success"); } Task.Delay(5000).Wait(); return(true); }
private bool DoRule(NaverVistiRule rule) { if (_naverRuleConfig.VisitRuleType == VisitRuleTypeEnum.PC) { return(DoPCRule(rule)); } else { Serilog.Log.Warning("mobile rule not support currently!"); return(false); } }
private bool VisitHomePage(NaverVistiRule rule) { if (!_webdriverAction.GotoUrl(UrlEndPointsOption.EngineUrl(_naverRuleConfig.VisitRuleType), _webDriver)) { Serilog.Log.Warning("home page visit failed"); return(false); } else { Serilog.Log.Information("home page visit success"); return(true); } }
private void SaveVisitRules() { var list = new List <NaverVistiRule>(); int i = 0; var dgvRoles = dgvVisitRule.Rows; foreach (DataGridViewRow row in dgvRoles) { i++; if (i == dgvRoles.Count) { break; } var rule = new NaverVistiRule(); rule.SearchKeys = row.Cells[0].Value?.ToString(); rule.WaitSecsMinInSearch = Convert.ToInt32(row.Cells[1].Value?.ToString()); rule.WaitSecsMaxInSearch = Convert.ToInt32(row.Cells[2].Value?.ToString()); rule.ProductKeys = row.Cells[3].Value?.ToString(); rule.WaitSecsMinInProduct = Convert.ToInt32(row.Cells[4].Value?.ToString()); rule.WaitSecsMaxInProduct = Convert.ToInt32(row.Cells[5].Value?.ToString()); rule.PriceKeys = row.Cells[6].Value?.ToString(); rule.WaitSecsMinInPrice = Convert.ToInt32(row.Cells[7].Value?.ToString()); rule.WaitSecsMaxInPrice = Convert.ToInt32(row.Cells[8].Value?.ToString()); list.Add(rule); } var pcVisitRules = JsonConvert.SerializeObject(list); saveFileDialog = new SaveFileDialog(); saveFileDialog.InitialDirectory = Directory.GetCurrentDirectory().Substring(0, 3); saveFileDialog.FileName = "visit_rule"; saveFileDialog.Filter = "visit_rule(*.db)|*.db|All Files(*.*)|*.*"; saveFileDialog.RestoreDirectory = true; if (saveFileDialog.ShowDialog() == DialogResult.OK) { string localFilePath = saveFileDialog.FileName; File.WriteAllText(localFilePath, pcVisitRules, Encoding.UTF8); Serilog.Log.Information("visit rules export success"); } else { Serilog.Log.Warning("visit rules export failed"); } }
private bool CacheVisitRules() { var list = new List <NaverVistiRule>(); int i = 0; var dgvRoles = dgvVisitRule.Rows; foreach (DataGridViewRow row in dgvRoles) { i++; if (i == dgvRoles.Count) { break; } var rule = new NaverVistiRule(); rule.SearchKeys = row.Cells[0].Value?.ToString(); rule.WaitSecsMinInSearch = Convert.ToInt32(row.Cells[1].Value?.ToString()); rule.WaitSecsMaxInSearch = Convert.ToInt32(row.Cells[2].Value?.ToString()); rule.ProductKeys = row.Cells[3].Value?.ToString(); rule.WaitSecsMinInProduct = Convert.ToInt32(row.Cells[4].Value?.ToString()); rule.WaitSecsMaxInProduct = Convert.ToInt32(row.Cells[5].Value?.ToString()); rule.PriceKeys = row.Cells[6].Value?.ToString(); rule.WaitSecsMinInPrice = Convert.ToInt32(row.Cells[7].Value?.ToString()); rule.WaitSecsMaxInPrice = Convert.ToInt32(row.Cells[8].Value?.ToString()); list.Add(rule); } if (list.Count > 0) { _naverVistiRules.Clear(); _naverVistiRules.AddRange(list); Serilog.Log.Information("cache visit rules success"); return(true); } else { Serilog.Log.Warning("no visit rules"); return(false); } }
private bool BrowserProductListAndVisitProduct(NaverVistiRule rule) { int currentPageNum = 0; //产品元素 IWebElement product = null; //检测翻页是否超限 if (currentPageNum > _naverRuleConfig.PageMax) { Serilog.Log.Warning("reach limit page max number"); return(false); } //获取页面内容 var respage = _webdriverAction.GetPage(_webDriver); if (string.IsNullOrEmpty(respage)) { return(false); } //判断当前页面是包含产品关键词 if (respage.Contains(rule.ProductKeys)) { var productXpath = "//li[@data-nv-mid='" + rule.ProductKeys + "']/div[@class='info']//a"; product = _webdriverAction.GetElementByXpath("", _webDriver); } //获取窗体高度 if (!_webdriverAction.GetClientHeight(_webDriver, out int clientHeight)) { return(false); } return(true); //try //{ // bool flag = false; // IWebElement product = null; // if (_WebDriver.PageSource.Contains(_CurrentVisitRule.ProductKey)) // { // //此处规则变化过 // product = _WebDriver.FindElement(By.XPath("//li[@data-nv-mid='" + _CurrentVisitRule.ProductKey + "']/div[@class='info']//a")); // } // else // LogHelper.Default.LogPrint($"当前页面不存在关键词:{_CurrentVisitRule.ProductKey}", 3); // if (product == null) // { // _CurrentPage++; // if (_CurrentPage >= _PCVisitControl.PageMax) // { // LogHelper.Default.LogPrint($"搜索超过最大页数,放弃继续翻页搜索", 3); // return false; // } // //下拉到最下边 // ScrollDown(_CurrentVisitRule.ProductListWaitSec * 1000); // //进行翻页 // flag = VisitPCNextPage(); // if (flag == false) // return false; // //递归方式查找产品 // flag = BrowserPCProductListAndVisitProduct(); // if (flag == false) // return false; // } // else // { // //浏览页面 // ScrollDownAndUp(_CurrentVisitRule.ProductListWaitSec * 1000); // //定位到指定元素 // ScrollToElement(_CurrentVisitRule.ProductListWaitSec * 500, product); // //获取产品连接并访问 // var url = product.GetAttribute("href"); // _WebDriver.Navigate().GoToUrl(url); // LogHelper.Default.LogPrint($"产品页面访问完成", 2); // Task.Delay(2000).Wait(); // //检测页面是否有权访问 // flag = AbormalPageCheck(); // if (flag == false) // return false; // } // return true; //} //catch (Exception ex) //{ // LogHelper.Default.LogDay($"BrowserPCProductListAndVisitProduct error,{ex}"); // LogHelper.Default.LogPrint($"BrowserPCProductListAndVisitProduct error,{ex.Message}", 4); // return false; //} }