/// <summary> /// 获取商品信息 /// </summary> /// <param name="waresId"></param> /// <returns></returns> public Wares GetWaresInfo(string waresId) { JDHttpHelper jdHttp = new JDHttpHelper(); jdHttp.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; Dictionary <string, string> headerDic = new Dictionary <string, string>(); headerDic["Accept-Encoding"] = "gzip, deflate, sdch, br"; headerDic["Accept-Language"] = "zh-CN,zh;q=0.8"; string url = string.Format("https://item.jd.com/{0}.html", waresId); string result = jdHttp.GetHtml(url, "get", Encoding.UTF8, headerDic); Match reg = Regex.Match(result, @"<div class=""sku-name"">(.*?)</div>", RegexOptions.Singleline); WPrice wp = null; this.GetWaresPrice(waresId).TryGetValue(waresId, out wp); if (wp == null) { wp = new WPrice() { p = "error!" }; } Wares wares = null; wares = new Wares() { url = url, id = waresId, title = reg.Groups[1].Value.Trim(), price = wp.p }; //查询价格 //https://p.3.cn/prices/mgets?callback=jQuery5638364&type=1&area=5_274_3207_0&pdtk=&pduid=938279766&pdpin=&pdbp=0&skuIds=J_4082633&source=item-pc //查询是否有货 //https://c0.3.cn/stock?skuId=4082633&area=5_274_3207_0&venderId=1000079304&cat=1318,1466,12122&buyNum=1&choseSuitSkuIds=&extraParam={%22originid%22:%221%22}&ch=1&fqsp=0&pduid=1982668971&pdpin=&callback=jQuery680992 return(wares); }
public Dictionary <string, WPrice> GetWaresPrice(string waresId) { /* * function a(o, e) { * function n() { * var o = readCookie("__jda"), * e = ""; * return o && o.indexOf(".") > -1 && (e = o.split(".")[1]), e * } * var t = o.skus || [], * i = o.$el || $("body"), * c = o.selector || ".J-p-", * a = o.text || "¥{NUM}"; * $.ajax({ * url: "//p.3.cn/prices/mgets", * data: { * skuids: "J_" + t.join(",J_"), * type: 2, * pdtk: readCookie("pdtk") || "", * pduid: n, * pdpin: readCookie("pin") || "", * pdbp: 0 * }, * dataType: "jsonp", * cache: !0, * success: function(o) { * for (var n in o) { * if (!o[n].id) return !1; * var t = o[n].id.replace("J_", ""); * parseFloat(o[n].p) > 0 ? i.find(c + t).html(a.replace("{NUM}", o[n].p)) : i.find(c + t).html("免费") * } * "function" == typeof e && e(o) * } * }) * } */ JDHttpHelper jdHttp = new JDHttpHelper(); Dictionary <string, string> headerDic = new Dictionary <string, string>(); headerDic["Accept-Encoding"] = "gzip, deflate, sdch, br"; headerDic["Accept-Language"] = "zh-CN,zh;q=0.8"; jdHttp.Referer = string.Format("https://item.jd.com/{0}.html", waresId); jdHttp.Accept = "*/*"; //Random rdm = new Random(); //string param1 = "jQuery" + Math.Floor(1e7 * rdm.NextDouble()).ToString(); string url = string.Format("https://p.3.cn/prices/mgets?skuIds=J_{0}&source=item-pc", waresId); string result = jdHttp.GetHtml(url, "get", Encoding.UTF8, headerDic); string pattern = @"{""id"":""J_(?<id>\d+)"",""p"":""(?<pvalue>[\d.]+)"",""m"":""(?<mvalue>[\d.]+)"",""op"":""(?<opvalue>[\d.]+)"""; MatchCollection m = Regex.Matches(result, pattern); Dictionary <string, WPrice> priceDic = new Dictionary <string, WPrice>(); foreach (Match item in m) { WPrice wp = new WPrice() { p = item.Groups["pvalue"].Value, m = item.Groups["mvalue"].Value, op = item.Groups["opvalue"].Value }; priceDic[item.Groups["id"].Value] = wp; } // return(priceDic); }