private void BtnCheckImage_Click(object sender, RoutedEventArgs e) { try { var shop = this.cbbShops.SelectedItem as ShopErp.Domain.Shop; if (shop == null) { throw new Exception("只有淘宝天猫可以进行匹配"); } string tadgetDomainCookies = CefCookieVisitor.GetCookieValues("tadget.taobao.com", null); string tbToken = CefCookieVisitor.GetCookieValues("tadget.taobao.com", "_tb_token_"); if (string.IsNullOrWhiteSpace(tbToken)) { throw new Exception("获取_tb_token cookie 为空"); } var url = new Uri("https://tadget.taobao.com/redaction/redaction/json.json?cmd=json_dirTree_query&count=true&_input_charset=utf-8&_tb_token_=" + tbToken.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries)[1]); string json = MsHttpRestful.SendTbData(System.Net.Http.HttpMethod.Get, url, null, tadgetDomainCookies, null); var rsp = Newtonsoft.Json.JsonConvert.DeserializeObject <ImageRsp>(json); var imageDirs = rsp.module.dirs.children.FirstOrDefault(obj => obj.name == "商品图片").children.Select(obj => obj.name).ToArray(); var allGoods = ServiceContainer.GetService <GoodsService>().GetByAll((this.cbbShops.SelectedItem as Shop).Id, GoodsState.UPLOADED, 0, DateTime.MinValue, DateTime.MinValue, "", "", GoodsType.GOODS_SHOES_NONE, "", ColorFlag.None, GoodsVideoType.NONE, "", "", "", 0, 0).Datas.OrderBy(obj => obj.VendorId).ToList(); var vendors = ServiceContainer.GetService <VendorService>().GetByAll("", "", "", "", 0, 0).Datas.ToList(); List <ShopErp.Domain.Goods> unMatchGoods = new List <ShopErp.Domain.Goods>(); List <string> unMatchDirs = new List <string>(); foreach (var g in allGoods) { var vendor = vendors.FirstOrDefault(obj => obj.Id == g.VendorId); if (vendor == null) { throw new Exception("商品未找到对应厂家:" + g.Number + " 商品ID:" + g.Id); } if (string.IsNullOrWhiteSpace(vendor.PingyingName)) { throw new Exception("厂家没有配置拼音:" + vendor.Name); } string nn = vendor.PingyingName.Trim() + "&" + g.Number.Trim(); var pg = imageDirs.FirstOrDefault(obj => obj.IndexOf(nn, StringComparison.OrdinalIgnoreCase) >= 0); if (pg == null) { unMatchGoods.Add(g); } } foreach (var pg in imageDirs) { string[] skus = pg.Split(','); foreach (var code in skus) { string[] nn = code.Split('&'); var g = allGoods.FirstOrDefault(obj => obj.Number.Equals(nn[1], StringComparison.OrdinalIgnoreCase) && vendors.FirstOrDefault(o => o.Id == obj.VendorId).PingyingName.Equals(nn[0], StringComparison.OrdinalIgnoreCase)); if (g == null) { unMatchDirs.Add(pg); } } } if (unMatchGoods.Count > 0 || unMatchDirs.Count > 0) { string msg1 = string.Join(",", unMatchGoods.Select(obj => vendors.FirstOrDefault(o => o.Id == obj.VendorId).Name + "&" + obj.Number)); string msg2 = string.Join(",", unMatchDirs); string msg = string.Format("系统中未在网站上匹配的:{0}\r\n,网站上未在系统中匹配的:{1}", msg1, msg2); MessageBox.Show(msg); Debug.WriteLine(msg); } else { MessageBox.Show("完全匹配"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private List <PopGoods> QueryHtmlGoodsPage(Shop shop, int pageIndex, PopGoodsState state) { Uri goodsListPageUri = new Uri(shop.PopType == PopType.TMALL ? "https://item.manager.tmall.com/tmall/manager/table.htm" : "https://item.publish.taobao.com/taobao/manager/table.htm"); string goodsEditPageDomain = shop.PopType == PopType.TMALL ? "item.upload.tmall.com" : "item.publish.taobao.com"; string goodsEditPageUrlBase = shop.PopType == PopType.TMALL ? "https://item.upload.tmall.com/tmall/publish.htm?id=" : "https://item.publish.taobao.com/sell/publish.htm?itemId="; var goodsListPageXsrfCookieValue = CefCookieVisitor.GetCookieValues(goodsListPageUri.Host, "XSRF-TOKEN"); if (string.IsNullOrWhiteSpace(goodsListPageXsrfCookieValue)) { throw new Exception("没有找到相应XSRF-TOKEN,刷新页面重新登录"); } var goodsListPageDomainCookie = CefCookieVisitor.GetCookieValues(goodsListPageUri.Host, null); if (string.IsNullOrWhiteSpace(goodsListPageDomainCookie)) { throw new Exception("未找到域:" + goodsListPageUri.Host + " 的任何COOKIE ,刷新页面重新登录"); } var goodsEditPageCookie = CefCookieVisitor.GetCookieValues(goodsEditPageDomain, null); string goodsState = state == PopGoodsState.NONE ? "all" : (state == PopGoodsState.ONSALE ? "on_sale" : "in_stock"); var xhrdata = "jsonBody=" + MsHttpRestful.UrlEncode("{\"filter\":{},\"pagination\":{\"current\":" + pageIndex + ",\"pageSize\":20},\"table\":{\"sort\":{}},\"tab\":\"" + goodsState + "\"}", Encoding.UTF8); var header = new Dictionary <string, string>(); header.Add("X-XSRF-TOKEN", goodsListPageXsrfCookieValue.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries)[1]); var content = MsHttpRestful.SendTbData(System.Net.Http.HttpMethod.Post, goodsListPageUri, header, goodsListPageDomainCookie, xhrdata); var resp = Newtonsoft.Json.JsonConvert.DeserializeObject <TaobaoQueryGoodsListResponse>(content); List <PopGoods> goods = new List <PopGoods>(); if (resp == null || resp.success == false) { throw new Exception("返回数据错误,请点击页面分页进行验证后重新开始"); } if (resp.data.table.dataSource == null) { return(goods); } foreach (var g in resp.data.table.dataSource) { if (this.WaitGoOn() == false) { break; } //每个商品获取的时候,都有可能,登录超时或者其它问题,需要循环 PopGoods pg = null; string goodsEditPageUrl = goodsEditPageUrlBase + g.itemId; for (int i = 0; i < 5; i++) { if (this.WaitGoOn() == false) { break; } Debug.WriteLine("开始抓取商品:" + g.itemId); this.OnDownload("暂停", true, "第:" + (i + 1) + " 次下载,第:" + pageIndex + "/" + ((resp.data.pagination.total / resp.data.pagination.pageSize) + 1) + "页,第" + (goods.Count + 1) + "条商品详情", false); bool ret = QueryHtmlGoods(g, goodsEditPageUrl, goodsEditPageCookie, ref pg); if (ret == true) { goods.Add(pg); break; } } if (this.isStop == false && pg == null) { throw new Exception("抓取商品5次都为成功:" + g.itemId); } if (this.WaitGoOn() == false) { break; } //每次等待3秒,太快淘宝会返回错误 Thread.Sleep(3000); } return(goods); }
private void MarkPopDelivery(string popOrderId, string deliveryCompany, string deliveryNumber) { string formId = "//form[@id = 'orderForm']"; string inputMark = "input"; //第一步读取页面信息 var uri = new Uri("https://wuliu.taobao.com/user/consign.htm?trade_id=" + popOrderId); string cookies = CefCookieVisitor.GetCookieValues(uri.Host, null); string html = MsHttpRestful.SendTbData(System.Net.Http.HttpMethod.Get, uri, null, cookies, null); //第二步 提取数据 Dictionary <string, string> paras = new Dictionary <string, string>(); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(html); var form = doc.DocumentNode.SelectSingleNode(formId); if (form == null) { Debug.WriteLine(form); throw new Exception("未在页面中找到:" + formId); } var inputNodes = doc.DocumentNode.SelectNodes("//input"); if (inputNodes == null || inputNodes.Count < 1) { throw new Exception("未找到任何:" + inputMark + " 元素"); } foreach (var v in inputNodes) { if (string.IsNullOrWhiteSpace(v.GetAttributeValue("name", ""))) { continue; } paras[v.GetAttributeValue("name", "")] = v.GetAttributeValue("value", ""); } paras.Remove("q"); paras.Remove("type"); paras.Remove("cat"); paras["companyName"] = "请输入物流公司名称"; paras["event_submit_do_offline_consign"] = "1"; paras["offlineNewFlag"] = "1"; paras["_fmw.r._0.c"] = paras["receiverContactName"]; paras["_fmw.r._0.adr"] = paras["receiverDetail"]; paras["_fmw.f._0.fe"] = ""; paras["_fmw.f._0.fet"] = ""; paras["_fmw.n._0.g"] = "您可以在此输入备忘信息(仅卖家自己可见)。"; paras["initialWeightOld"] = "all"; //进行几种常见错误的检测 if (paras.ContainsKey("receiverZipCode") == false || paras["receiverZipCode"].Length != 6) { throw new Exception("邮政编码不为6位,当前值:" + paras["receiverZipCode"]); } if (paras.ContainsKey("receiverDetail") == false || paras["receiverDetail"].Length < 4) { throw new Exception("详细地址必须大于等于4个字,当前值:" + paras["receiverDetail"]); } paras["mailNo"] = deliveryNumber; paras["companyCode"] = deliveryCompany; //生成数据,该页面使用GBK编码 List <string> ss = paras.Select(obj => obj.Key + "=" + MsHttpRestful.UrlEncode(obj.Value, Encoding.GetEncoding("GBK"))).ToList(); string urlEncodeData = string.Join("&", ss); string ret = MsHttpRestful.SendTbData(System.Net.Http.HttpMethod.Post, uri, null, cookies, urlEncodeData); if (ret.Contains("恭喜您,操作成功")) { return; } if (ret.Contains("运单号不符合规则或已经被使用")) { throw new Exception("运单号不符合规则或已经被使用"); } throw new Exception("返回页面数据无法识别,请查看相应结果"); }