コード例 #1
0
        private IEnumerable<Domain.Product> Scrape(string postcode)
        {
            var output = new List<Domain.Product>();

            using (var driver = new ChromeDriver())
            {
                driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));

                driver.Navigate().GoToUrl("http://www.dominos.co.uk");

                driver.FindElementById("txtPostcode").SendKeys(postcode);

                driver.FindElementById("btnStoreSearch").Click();

                Thread.Sleep(1000);

                driver.FindElementByCssSelector(".btn-secondary").Click();

                Thread.Sleep(1000);

                driver.Navigate().GoToUrl("https://www.dominos.co.uk/ProductCatalog/GetStoreContext");

                string storeContextJson = driver.FindElementByTagName("pre").Text;

                var storeContext = JsonConvert.DeserializeObject<StoreContext>(storeContextJson);

                driver.Navigate().GoToUrl(
                    $"https://www.dominos.co.uk/ProductCatalog/GetStoreCatalog?collectionOnly={storeContext.SessionContext.CollectionOnly}&menuVersion={storeContext.SessionContext.MenuVersion}&storeId={storeContext.SessionContext.StoreId}");

                string storeCatalogJson = driver.FindElementByTagName("pre").Text;

                var products = JsonConvert.DeserializeObject<List<DTOs.Category>>(storeCatalogJson);

                output = products
                    .SelectMany(p => p.Subcategories)
                    .SelectMany(p => p.Products)
                    .Where(p => !p.IsAlcohol)
                    .Where(p => p.Type != "PizzaLegend")
                    .Where(p => p.Name != "Half & Half")
                    .Where(p => p.Name != "Create Your Own")
                    .Select(p => new Domain.Product()
                        {
                            Id = p.Id.ToString(),
                            Name = p.Name,
                            Description = p.Description,
                            ImageUrl = p.ImageUrl.Replace("256x256", "1024x1024"),
                            GlutenFree = p.IsGlutenFree,
                            Hot = p.IsHot,
                            Vegetarian = p.IsVegetarian,
                            Price = p.DisplayPrice,
                            Category = (ProductCategory)Enum.Parse(typeof(ProductCategory), p.Type)
                        })
                    .ToList();
            }

            return output;
        }