private string BuildAjaxSearchUrl(string webContent, IDictionary <string, string> renderStructure) { var matchResults = Regex.Match(webContent, "(?<=var params = ){[^}]+}"); if (!matchResults.Success) { throw new NotSupportedException("无法从页面中解析出搜索参数"); } var jObject = JObject.Parse(matchResults.Value); var navigator = HtmlDocumentHelper.CreateNavigator(webContent); //System.Func<string, string> readJsonFunc = key => JsonHelper.TryReadJobjectValue(jObject, key,(string)null); //System.Func<string, string> readHtmlFunc = key => //{ // string value; // renderStructure.TryGetValue(key,out value); // return value; //}; //System.Func<string, string> readInputFunc = key => //HtmlDocumentHelper.GetNodeValue(navigator,$@"//input[@id='{key}']/@value"); var collection = Url.CreateQueryCollection(); collection[@"appId"] = ReadJsonFunc(jObject, "appId"); collection[@"orderBy"] = "5"; collection[@"pageNo"] = "1"; collection[@"direction"] = "1"; collection[@"categoryId"] = ReadJsonFunc(jObject, @"categoryId"); collection[@"pageSize"] = @"24"; collection[@"pagePrototypeId"] = ReadJsonFunc(jObject, @"pagePrototypeId"); collection[@"pageInstanceId"] = ReadHtmlFunc(renderStructure, @"m_render_pageInstance_id"); collection[@"moduleInstanceId"] = ReadHtmlFunc(renderStructure, "m_render_instance_id"); collection[@"prototypeId"] = ReadHtmlFunc(renderStructure, @"m_render_prototype_id"); collection[@"templateId"] = ReadHtmlFunc(renderStructure, @"m_render_template_id"); collection[@"layoutInstanceId"] = ReadHtmlFunc(renderStructure, @"m_render_layout_instance_id"); collection[@"origin"] = ReadHtmlFunc(renderStructure, @"m_render_origin"); collection[@"shopId"] = ReadInputFunc(navigator, @"shop_id"); collection[@"verderId"] = ReadInputFunc(navigator, @"vender_id"); collection[@"_"] = $"{JsCodeHelper.GetDateTime()}"; var baseUrl = renderStructure[@"m_render_is_search"] == "true" ? @"http://module-jshop.jd.com/module/getModuleHtml.html" : @"http://mall.jd.com/view/getModuleHtml.html"; return(Url.CombinUrl(baseUrl, collection)); }
/// <summary> /// 构造搜索 ajax url /// </summary> /// <param name="webContent">Content of the web.</param> /// <param name="renderStructure">The render structure.</param> /// <returns></returns> /// <exception cref="System.NotSupportedException">$无法从页面中解析出搜索参数</exception> private string BuildAjaxSearchUrl(string webContent, IDictionary <string, string> renderStructure) { /*webContent = File.ReadAllText(@"C:\Users\sinoX\Desktop\京东搜索页.html");*/ var matchResults = Regex.Match(webContent, "(?<=var params = ){[^}]+}"); if (!matchResults.Success) { throw new NotSupportedException("无法从页面中解析出搜索参数"); } // {"appId":"435517","orderBy":"5","direction":"0","categoryId":"0","pageSize":"24","venderId":"1000004373","isGlobalSearch":"0","maxPrice":"0","pagePrototypeId":"17","pageNo":"1","shopId":"1000004373","minPrice":"0"} var jObject = JObject.Parse(matchResults.Value); var navigator = HtmlDocumentHelper.CreateNavigator(webContent); System.Func <string, string> readJsonFunc = key => JsonHelper.TryReadJobjectValue(jObject, key, (string)null); System.Func <string, string> readHtmlFunc = key => { string value; renderStructure.TryGetValue(key, out value); return(value); }; System.Func <string, string> readInputFunc = key => HtmlDocumentHelper.GetNodeValue(navigator, $@"//input[@id='{key}']/@value"); var collection = Url.CreateQueryCollection(); collection[@"appId"] = readJsonFunc("appId"); collection[@"orderBy"] = "5"; collection[@"pageNo"] = "1"; collection[@"direction"] = "1"; collection[@"categoryId"] = readJsonFunc(@"categoryId"); collection[@"pageSize"] = @"24"; collection[@"pagePrototypeId"] = readJsonFunc(@"pagePrototypeId"); collection[@"pageInstanceId"] = readHtmlFunc(@"m_render_pageInstance_id"); collection[@"moduleInstanceId"] = readHtmlFunc(@"m_render_instance_id"); collection[@"prototypeId"] = readHtmlFunc(@"m_render_prototype_id"); collection[@"templateId"] = readHtmlFunc(@"m_render_template_id"); collection[@"layoutInstanceId"] = readHtmlFunc(@"m_render_layout_instance_id"); collection[@"origin"] = readHtmlFunc(@"m_render_origin"); collection[@"shopId"] = readInputFunc(@"shop_id"); collection[@"venderId"] = readInputFunc(@"vender_id"); /*collection[@"callback"] = @"jshop_module_render_callback"; // 不用这个直接返回一个 json 结构 */ collection[@"_"] = $"{JsCodeHelper.GetDateTime()}"; var baseUrl = renderStructure[@"m_render_is_search"] == "true" ? @"http://module-jshop.jd.com/module/getModuleHtml.html" : @"http://mall.jd.com/view/getModuleHtml.html"; return(Url.CombinUrl(baseUrl, collection)); }
/// <summary> /// 更新价格列表 /// </summary> /// <param name="resultList">The result list.</param> /// <param name="skuIdKey">The sku identifier key.</param> private void UpdateResultsPrices(List <IResut> resultList, string skuIdKey) { if (resultList.Count == 0) { return; } IDictionary <string, IResut> resultDictionary = new Dictionary <string, IResut>(); resultList.ForEach(result => resultDictionary[$"J_{result[skuIdKey]}"] = result); /*foreach(var result in resultList) * { * var keyname = result[skuIdKey]; * resultDictionary[$"J_{keyname}"] = result * }*/ var skuids = new string[resultDictionary.Count]; resultDictionary.Keys.CopyTo(skuids, 0); var collection = Url.CreateQueryCollection(); collection[@"skuids"] = string.Join(",", skuids); collection[@"_"] = $"{JsCodeHelper.GetDateTime()}"; // http://p.3.cn/prices/mgets?skuids=J_1077038109,J_10134047427,J_10344905938,J_10377204212&type=2&callback=callBackPriceService&_=1466737672671 const string BaseUrl = @"http://p.3.cn/prices/mgets"; var url = Url.CombinUrl(BaseUrl, collection); var webContent = this.GetWebContent(url); var jArray = JArray.Parse(webContent); foreach (var jToken in jArray) { var skuid = JsonHelper.ReadJobjectValue <string>(jToken, @"id"); var price = JsonHelper.ReadJobjectValue <string>(jToken, @"p"); var mprice = JsonHelper.ReadJobjectValue <string>(jToken, @"m"); IResut result; if (!resultDictionary.TryGetValue(skuid, out result)) { continue; } result[@"ProductPrice"] = price; result[@"ProductMPrice"] = mprice; } }
/// <summary> /// 更新价格 /// </summary> /// <param name="resultList"></param> /// <param name="skuIdKey"></param> private void UpdateResultsPrices(List <IResut> resultList, string skuIdKey) { if (resultList.Count == 0) { return; } IDictionary <string, IResut> resultDictionary = new Dictionary <string, IResut>(); resultList.ForEach(result => resultDictionary[$"J_{result[skuIdKey]}"] = result); var skuids = new string[resultDictionary.Count]; resultDictionary.Keys.CopyTo(skuids, 0); var collection = Url.CreateQueryCollection(); collection[@"skuids"] = string.Join(",", skuids); collection[@"_"] = $"{JsCodeHelper.GetDateTime()}"; const string BaseUrl = @"http://p.3.cn/prices/mgets"; var url = Url.CombinUrl(BaseUrl, collection); var webContent = this.GetWebContent(url); var jArray = JArray.Parse(webContent); foreach (var jToken in jArray) { var skuid = JsonHelper.ReadJobjectValue <string>(jToken, @"id"); var price = JsonHelper.ReadJobjectValue <string>(jToken, @"p"); var mprice = JsonHelper.ReadJobjectValue <string>(jToken, @"m"); IResut result; if (!resultDictionary.TryGetValue(skuid, out result)) { continue; } result[@"ProductPrice"] = price; result[@"ProductMPrice"] = mprice; } }