public void ReadAllItem() { string url = "https://www.tkyfw.com/Ching_slist_{0}.html"; for (int i = 0; i < 30000; i++) { string content = request.HttpGet(string.Format(url, i), ""); content = CommonFun.GetValue(content, "<div class=\"buying\">", "</div>"); MatchCollection ms = CommonFun.GetValues(content, "<td class=\"td3\">", "</td>"); if (!string.IsNullOrEmpty(ms[0].Value)) { try { ItemInfo item = new ItemInfo(); item.Name = ms[0].Value; item.ID = ms.Count > 3 ? ms[2].Value : ""; item.Created = CommonFun.GetValue(content, "rel=\"theqiye\">", "</td>"); string priceStr = CommonFun.GetValue(content, "<strong class=\"value yahei\">", "</strong>"); try { if (string.IsNullOrEmpty(priceStr)) { Console.WriteLine("Price is null ID;{0}, url:{1}", item.ID, string.Format(url, i)); continue; } item.ShopPrice = Convert.ToDecimal(priceStr); } catch (Exception ex) { Console.WriteLine(ex.ToString() + priceStr); } item.Format = CommonFun.GetValue(content, "<li class=\"cur\">", "</li>"); item.Format = CommonFun.GetValue(item.Format, "\">", "</a>"); string key = item.ID + "{" + item.Format + "}"; if (allItems.ContainsKey(key)) { if (allItems[key].ShopPrice > item.ShopPrice) { allItems[key] = item; } } else { allItems.Add(key, item); } CommonFun.WriteCSV("TK.csv", item); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } }
private static void ComparePrice(BaseItemInfo platformItem, BaseItemInfo info, string fileName) { if (info.ShopPrice * (decimal)1.25 <= platformItem.ShopPrice) { info.ID = platformItem.ID; info.PlatformPrice = platformItem.ShopPrice; info.ViewCount = platformItem.ViewCount; info.Type = platformItem.Type; CommonFun.WriteCSV(fileName + "/25以上.csv", info); } else if (info.ShopPrice * (decimal)1.15 <= platformItem.ShopPrice && platformItem.ShopPrice * (decimal)1.25 > platformItem.ShopPrice) { info.ID = platformItem.ID; info.PlatformPrice = platformItem.ShopPrice; info.ViewCount = platformItem.ViewCount; info.Type = platformItem.Type; CommonFun.WriteCSV(fileName + "/15-25.csv", info); } else if (info.ShopPrice * (decimal)1.05 <= platformItem.ShopPrice && platformItem.ShopPrice * (decimal)1.15 > platformItem.ShopPrice) { info.ID = platformItem.ID; info.PlatformPrice = platformItem.ShopPrice; info.ViewCount = platformItem.ViewCount; info.Type = platformItem.Type; CommonFun.WriteCSV(fileName + "/5-15.csv", info); } else if (info.ShopPrice == platformItem.ShopPrice && info.ShopPrice * (decimal)1.05 > platformItem.ShopPrice) { info.ID = platformItem.ID; info.PlatformPrice = platformItem.ShopPrice; info.ViewCount = platformItem.ViewCount; info.Type = platformItem.Type; CommonFun.WriteCSV(fileName + "/0-5.csv", info); } else { info.ID = platformItem.ID; info.PlatformPrice = platformItem.ShopPrice; info.ViewCount = platformItem.ViewCount; info.Type = platformItem.Type; CommonFun.WriteCSV(fileName + "/0以下.csv", info); } }
private void CreateItemOtherInfo(object value) { string temp = null; try { string name = (string)value; if (name.Contains("注射")) { return; } HttpRequest tempRequest = new HttpRequest(); string url = string.Format("http://www.yaofangwang.com/search.html?keyword={0}", System.Web.HttpUtility.UrlEncode(name)); string content = tempRequest.HttpGetPlatform(url); List <string> items = GetItemStr(content); foreach (string item in items) { bool result = false; do { try { temp = item; ItemInfo info = new ItemInfo(); info.Format = CommonFun.GetValue(item, "规格:", "<"); info.Created = CommonFun.GetValue(item, "生产厂家:", "<"); //库存 info.ID = CommonFun.GetValue(item, "批准文号:", "<"); string priceStr = CommonFun.GetValue(item, "¥", "<"); info.ShopPrice = string.IsNullOrEmpty(priceStr) ? 0 : Convert.ToDecimal(priceStr); content = tempRequest.HttpGetPlatform(CommonFun.GetValue(item, "<a target=\"_blank\" href=\"", "\"")); int startIndex = content.IndexOf("<div class=\"share clearfix\">"); int endIndx = content.IndexOf("id=\"priceA\">"); content = content.Substring(startIndex, endIndx - startIndex); info.ViewCount = CommonFun.GetValue(content, "<dt>最近浏览</dt><dd class=\"w1\">", "次"); info.Name = CommonFun.GetValue(content, "<dd class=\"w2 l\"><strong>", "<"); MatchCollection ms = CommonFun.GetValues(content, "<dd class=\"w3 l\">", "</"); info.Type = ms[1].Value; string key = info.ID + "{" + info.Format + "}"; if (platformItems.ContainsKey(key)) { if (platformItems[key].ShopPrice > info.ShopPrice) { platformItems[key] = info; } } else { platformItems.Add(key, info); } CommonFun.WriteCSV("平台信息.csv", info); result = true; } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } while (!result); } Console.WriteLine("finishName;{0}, finishCoutn:{1}, totalCount:{2}", name, ++finishCount, toltalCount); if (finishCount == toltalCount) { Console.WriteLine("Finished !!!!!!!!!!!!!!!!!"); } } catch (Exception ex) { Console.WriteLine("content;{0}, error:{1}", temp, ex.ToString()); } }
public void Compare(string pFileName, string sFileName, string createDir, bool isload = false) { ReadPlatFormWebPageValue flatform = new ReadPlatFormWebPageValue(); BaseReadWebPage platform = new BaseReadWebPage(); BaseReadWebPage shop = new BaseReadWebPage(); if (!string.IsNullOrEmpty(pFileName)) { platform.ReadBaseItemInfo(pFileName, true); } shop.ReadBaseItemInfo(sFileName, true); foreach (BaseItemInfo item in shop.ShopAllItems.Values) { bool isEixst = false; string itemName = item.Name; if (item.Name.Contains("】")) { int itemNameIndex = item.Name.LastIndexOf("】") + 1; itemName = item.Name.Substring(itemNameIndex, item.Name.Length - itemNameIndex); } if (itemName.Contains("(")) { int itemNameIndex = itemName.IndexOf('('); itemName = itemName.Substring(0, itemNameIndex); } foreach (BaseItemInfo platformItem in platform.ShopAllItems.Values) { bool isSame = false; if (!string.IsNullOrEmpty(item.ID) && !string.IsNullOrEmpty(platformItem.ID)) { if (item.ID.Trim() == platformItem.ID.Trim()) { isSame = true; } } if (isSame || (itemName == platformItem.Name && item.Created == platformItem.Created)) { if (CommonFun.IsSameFormat(platformItem.Format, item.Format, platformItem.Name, item.Name)) { isEixst = true; if (ComparePrice(platformItem, item)) { break; } } } } if (!isEixst && isload) { List <BaseItemInfo> seachItems = flatform.SeachInfoByID(item.ID); Dictionary <string, BaseItemInfo> saveInfos = new Dictionary <string, BaseItemInfo>(); foreach (BaseItemInfo info in seachItems) { string key = info.Name + info.Format + info.Created; if (!platform.ShopAllItems.ContainsKey(key)) { if (item.ID == info.ID || (itemName == info.Name && item.Created == info.Created)) { if (CommonFun.IsSameFormat(info.Format, item.Format, info.Name, item.Name)) { isEixst = true; ComparePrice(info, item); } } platform.ShopAllItems.Add(key, info); saveInfos.Add(key, info); } else if (platform.ShopAllItems[key].ShopSelaPrice > info.ShopSelaPrice) { if (item.ID == info.ID || (itemName == info.Name && item.Created == info.Created)) { if (CommonFun.IsSameFormat(info.Format, item.Format, info.Name, item.Name)) { isEixst = true; ComparePrice(info, item); } } platform.ShopAllItems[key] = info; saveInfos[key] = info; } } foreach (BaseItemInfo saveInfo in saveInfos.Values) { string name = string.IsNullOrEmpty(pFileName) ? "Platform/Platform" + ticks + ".csv" : pFileName; CommonFun.WriteCSV(name, saveInfo); } } if (!isEixst) { CommonFun.WriteCSV(createDir + "/NotIsEixst" + ticks + ".csv", item); } } }
/// <summary> /// 对比价格(在线数据) /// </summary> public void OnlineComparePrice() { ReadPlatFormWebPageValue flatform = new ReadPlatFormWebPageValue(); Dictionary <string, BaseItemInfo> platItems = new Dictionary <string, BaseItemInfo>(); Dictionary <string, List <BaseItemInfo> > seachItems = new Dictionary <string, List <BaseItemInfo> >(); Dictionary <string, string> seachedItemName = new Dictionary <string, string>(); foreach (BaseItemInfo item in ShopAllItems.Values) { string key = item.Name + item.Format + item.Created; //查找该商品 if (!seachedItemName.ContainsKey(item.Name)) { seachedItemName.Add(item.Name, item.Name); List <BaseItemInfo> item_list = flatform.SeachInfoByID(item.Name); Dictionary <string, BaseItemInfo> minPricItems = new Dictionary <string, BaseItemInfo>(); foreach (BaseItemInfo sItem in item_list) { string sItemKey = sItem.Name + sItem.Format + sItem.Created; if (!platItems.ContainsKey(sItemKey)) { platItems.Add(sItemKey, sItem); minPricItems.Add(sItemKey, sItem); } else if (platItems[sItemKey].ShopSelaPrice > sItem.ShopSelaPrice) { platItems[sItemKey] = sItem; minPricItems[sItemKey] = sItem; } } seachItems.Add(item.Name, minPricItems.Values.ToList()); } //对比价格 if (seachItems.ContainsKey(item.Name)) { List <BaseItemInfo> compareItems = seachItems[item.Name]; bool isExist = false; foreach (BaseItemInfo compareItem in compareItems) { if (item.Created == compareItem.Created) { if (CommonFun.IsSameFormat(item.Format, compareItem.Format, item.Name, compareItem.Name)) { isExist = true; //价格对比 if (item.ShopPrice > 0 && item.ShopPrice * (decimal)0.75 >= compareItem.ShopSelaPrice) { //浏览量对比 if (!string.IsNullOrEmpty(compareItem.ViewCount) && Convert.ToInt32(compareItem.ViewCount) >= 500) { item.ID = compareItem.ID; item.ShopSelaPrice = compareItem.ShopSelaPrice; item.Type = compareItem.Type; item.ViewCount = compareItem.ViewCount; CommonFun.WriteCSV("25%" + ticks + ".csv", item); } } break; } } } if (!isExist) { CommonFun.WriteCSV("YaoTu/NoFormat" + ticks + ".csv", item); } } else { CommonFun.WriteCSV("YaoTu/NotExist" + ticks + ".csv", item); } } foreach (BaseItemInfo item in platItems.Values) { CommonFun.WriteCSV("YaoTu/Plateform" + ticks + ".csv", item); } }
public void WriteItem(BaseItemInfo item, string fileName) { CommonFun.WriteCSV(fileName, item); }