/// <summary> /// 执行业务 /// </summary> private void DoBusiness(_1688RuleConfig ruleConfig) { bool flag = false; //访问URL地址 flag = webdriverAction.GotoUrl(ruleConfig.BeginUrl, webDriver); if (!flag) { return; } //输入关键词 flag = webdriverAction.DoInput(ruleConfig.Keyword, "home-header-searchbox", webDriver); if (!flag) { return; } //回车进行搜索 flag = webdriverAction.SendKeys(Keys.Enter, "//button[@class='single']", webDriver); if (!flag) { return; } //提取页面数据 CollectGoodsItems(ruleConfig); }
/// <summary> /// 获取商品信息 /// </summary> /// <param name="ruleConfig"></param> /// <returns></returns> private bool CollectGoodsItems(_1688RuleConfig ruleConfig) { //TODO:应该进行页面循环 //识别下一页位置 var nextPageEle = webdriverAction.GetElementByXpath("//a[@class='fui-next']", webDriver); if (nextPageEle == null) { Serilog.Log.Warning("翻页元素查找失败"); return(false); } //滚动到翻页元素 var nextPageScrollFlag = webdriverAction.ScrollToElement(nextPageEle, webDriver); if (!nextPageScrollFlag) { return(false); } Thread.Sleep(1000); //获取当前页面商品列//div[@class='img]表 var goodsItemBlocks = webdriverAction.GetEelements("//div[contains(@class,'common-offer-card')]", webDriver); if (goodsItemBlocks.flag == false) { return(false); } //识别商品信息 ParseGoodsInfo(goodsItemBlocks.eles); return(true); }
/// <summary> /// 开始运行 /// </summary> private void Run() { try { //TODO:载入配置文件 var ruleConfig = new _1688RuleConfig(); //初始化 if (!InitWebdriver()) { return; } //执行业务 DoBusiness(ruleConfig); } catch (Exception ex) { Serilog.Log.Error(ex, "1688Service error"); driverHelper.CloseWedriver(webDriver); } }