Пример #1
0
 public Price Search(string url)
 {
     //var url = Helper.CheckUrl(link);
     if (string.IsNullOrWhiteSpace(url))
     {
         return(null);
     }
     if (url.Contains("product.suning.com"))
     {
         driver.Navigate().GoToUrl(url);
         var price = SUNINGPriceParser.ExtractPrice(driver);
         Console.WriteLine(JsonConvert.SerializeObject(price));
         return(price);
     }
     else if (url.StartsWith("https://item.jd.com/"))
     {
         //driver.Navigate().GoToUrl(url);
         //var text = driver.FindElement(By.ClassName("summary-price-wrap")).Text;
         //var title = driver.FindElement(By.ClassName("sku-name")).Text;
         //var price = ParsePrice(text, url, outputPath);
     }
     else if (url.StartsWith("https://detail.tmall.com/item.htm?") || url.StartsWith("https://world.tmall.com/item/"))
     {
         driver.Navigate().GoToUrl(url);
         if (!login)
         {
             Console.WriteLine("Please login TMall");
             Console.ReadKey();
             login = true;
         }
         var priceText = "";
         var title     = "";
         var storeName = "";
         try
         {
             priceText = driver.FindElement(By.ClassName("tm-fcs-panel")).Text;
             title     = driver.FindElement(By.ClassName("tb-detail-hd")).FindElement(By.TagName("h1")).Text;
             storeName = driver.FindElement(By.ClassName("shopLink")).Text;
         }
         catch (NoSuchElementException e)
         {
             Console.WriteLine(e.Message);
         }
         if (String.IsNullOrWhiteSpace(priceText))
         {
             return(null);
         }
         var price = ParsePrice(priceText, url);
         price.sourceUrl = url;
         price.storeName = storeName;
         price.ItemName  = title;
         price.Calculate();
         Console.WriteLine(url);
         Console.WriteLine(priceText);
         Console.WriteLine(title);
         Console.WriteLine(JsonConvert.SerializeObject(price));
         return(price);
     }
     return(null);
 }
Пример #2
0
        public static Price ExtractPrice(IWebDriver driver)
        {
            var priceText = driver.FindElement(By.Id("priceDom")).Text;
            var title     = driver.FindElement(By.Id("itemDisplayName")).Text;
            var storeInfo = driver.FindElement(By.ClassName("si-intro-list")).Text;
            var storeName = SUNINGPriceParser.ParseShopName(storeInfo);

            Console.WriteLine("store: " + storeName);
            var price = Parse(priceText, driver.Url);

            price.sourceUrl = driver.Url;
            price.storeName = storeName;
            price.ItemName  = title;
            price.Calculate();
            return(price);
        }
Пример #3
0
        //public List<Price> GetSmzdmItems(Option option)
        //{
        //    var deals = new List<Price>();
        //    var pages = new List<string>();
        //    foreach (var pn in option.pageNumbers.Split(','))
        //    {
        //        pages.Add("https://www.smzdm.com/jingxuan/p" + pn + "/");
        //    }
        //    foreach (var page in pages)
        //    {
        //        driver.Navigate().GoToUrl(page);

        //        var items = driver.FindElements(By.ClassName("z-feed-content")).ToList();
        //        Console.WriteLine(items.Count);
        //        var SmzdmList = new List<Dictionary<string, string>>();

        //        foreach (IWebElement item in items)
        //        {
        //            Console.WriteLine(item.Text);
        //            var oldPrice = 0.0;
        //            var lines = item.Text.Split('\n');
        //            var itemTitle = "";
        //            if (lines.Length >= 2)
        //            {
        //                if (lines[0].IndexOf("件") != -1 || lines[1].IndexOf("合") != -1)
        //                {
        //                    Console.WriteLine("complicated price skip ");
        //                    continue;
        //                }
        //                int idx = lines[1].IndexOf("元");
        //                if (idx != -1)
        //                {
        //                    var priceStr = lines[1].Substring(0, idx);
        //                    try
        //                    {
        //                        oldPrice = double.Parse(Helper.ParseDigits(priceStr));
        //                        if (oldPrice < 10) continue;
        //                    }
        //                    catch (System.FormatException)
        //                    {
        //                        continue;
        //                    }
        //                    itemTitle = lines[0];
        //                    Console.WriteLine("old price " + oldPrice);
        //                }
        //            }
        //            else
        //            {
        //                continue;
        //            }

        //            var elements = item.FindElements(By.TagName("a")).ToList();
        //            var goSmzdmUrl = "";
        //            var itemSmzdmUrl = "";
        //            foreach (IWebElement e in elements)
        //            {
        //                var href = e.GetAttribute("href");
        //                if (goSmzdmUrl == "" && !string.IsNullOrWhiteSpace(href) && href.StartsWith("https://go.smzdm.com/"))
        //                {
        //                    goSmzdmUrl = href;
        //                    Console.WriteLine(href);
        //                }
        //                else if (itemSmzdmUrl == "" && !string.IsNullOrWhiteSpace(href) && href.StartsWith("https://www.smzdm.com/p/") && !href.EndsWith("comment"))
        //                {
        //                    itemSmzdmUrl = href;
        //                    Console.WriteLine(href);
        //                }

        //                if (goSmzdmUrl != "" && itemSmzdmUrl != "")
        //                {
        //                    var it = new Dictionary<string, string>();
        //                    it.Add("smzdmProduct", itemSmzdmUrl);
        //                    it.Add("smzdmGo", goSmzdmUrl);
        //                    it.Add("smzdmGoodPrice", oldPrice.ToString());
        //                    it.Add("smzdmItemTitle", itemTitle);
        //                    SmzdmList.Add(it);
        //                    break;
        //                }
        //            }

        //        }

        //        foreach (var it in SmzdmList)
        //        {
        //            if (Helper.ToUrl(driver,it["smzdmProduct"]))
        //            {
        //                try
        //                {
        //                    driver.FindElement(By.ClassName("new-baike-card"));
        //                    if (Helper.ToUrl(driver, it["smzdmGo"]))
        //                    {
        //                        var p = new Price();
        //                        p.SmzdmGoodPrice = double.Parse(it["smzdmGoodPrice"]);
        //                        p.sourceUrl = driver.Url;
        //                        p.SmzdmItemTitle = it["smzdmItemTitle"];
        //                        p.SmzdmGoUrl = it["smzdmGo"];
        //                        deals.Add(p);
        //                        Console.WriteLine("added " + p.sourceUrl + " " + p.SmzdmGoodPrice);
        //                    }
        //                }
        //                catch (NoSuchElementException e)
        //                {
        //                    Console.WriteLine("No Baike, drop " + it["smzdmProduct"]);
        //                }
        //            }
        //        }
        //    }
        //    return deals;
        //}


        public Price ParsePrice(string text, string source)
        {
            if (source.Contains("suning.com"))
            {
                return(SUNINGPriceParser.Parse(text, source));
            }
            else if (source.Contains("item.jd.com"))
            {
                return(JDPriceParser.Parse(text, source));
            }
            else if (source.Contains("tmall.com"))
            {
                return(TMPriceParser.Parse(text, source));
            }
            return(null);
        }
Пример #4
0
        public Price CheckPrice(Dictionary <string, string> it)
        {
            try
            {
                if (it == null)
                {
                    return(null);
                }
                if (jdCount > 10)
                {
                    this.driver.Quit();
                    this.driver = new FirefoxDriver();
                    jdCount     = 0;
                }
                if (Helper.ToUrl(driver, it["smzdmProduct"]))
                {
                    Console.WriteLine(driver.Url);
                    try
                    {
                        var url = "";
                        driver.FindElement(By.ClassName("new-baike-card"));
                        if (Helper.ToUrl(driver, it["smzdmGo"]))
                        {
                            Console.WriteLine(driver.Url);
                            url = driver.Url;
                            var price = new Price();
                            if (driver.Url.StartsWith("https://product.suning.com/") || driver.Url.StartsWith("http://product.suning.com/"))
                            {
                                price = SUNINGPriceParser.ExtractPrice(driver);
                                if (price != null)
                                {
                                    price.Calculate();
                                    if (price.finalPrice <= 0)
                                    {
                                        Console.WriteLine("Fail to process SUNING item, Skip");
                                        return(null);
                                    }
                                }
                            }
                            else if (driver.Url.StartsWith("https://re.jd.com/cps/item/") || driver.Url.StartsWith("http://re.jd.com/cps/item/"))
                            {
                                jdCount++;
                                driver.FindElement(By.ClassName("gobuy")).Click();
                                Console.WriteLine("Wait 5s for redirect from re.jd.com");
                                Thread.Sleep(5000);
                                if (driver.Url.StartsWith("https://passport.jd.com/"))
                                {
                                    var parts = driver.Url.Split("?=".ToCharArray());
                                    foreach (var p in parts)
                                    {
                                        if (p.StartsWith("https://item.jd.com/"))
                                        {
                                            driver.Navigate().GoToUrl(p);
                                            price = JDPriceParser.ExtractPrice(driver);

                                            break;
                                        }
                                    }
                                    //driver.Close();
                                }
                            }
                            else if (driver.Url.StartsWith("https://item.jd.com/") || driver.Url.StartsWith("http://item.jd.com/"))
                            {
                                jdCount++;
                                price = JDPriceParser.ExtractPrice(driver);
                                //driver.Close();
                                //Console.WriteLine("close page2");
                            }
                            //if(driver.Url.StartsWith("https://item.jd.com/") || driver.Url.StartsWith("http://item.jd.com/"))
                            //{
                            //    price = JDPriceParser.ExtractPrice();
                            //}
                            price.SmzdmGoodPrice = double.Parse(it["smzdmGoodPrice"]);
                            price.sourceUrl      = url;
                            price.SmzdmItemTitle = it["smzdmItemTitle"];
                            price.SmzdmGoUrl     = it["smzdmGo"];

                            Console.WriteLine("Verfied " + price.sourceUrl + " " + price.SmzdmGoodPrice);

                            return(price);
                        }
                    }
                    catch (NoSuchElementException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }
            catch (WebDriverException e)
            {
                Console.WriteLine("deal finder " + e.Message);
                return(null);
            }
            return(null);
        }