示例#1
0
        public void ScrapeStockData()
        {
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
            IList <IWebElement> stockData           = driver.FindElements(By.ClassName("simpTblRow"));

            Console.WriteLine("Total stocks: " + stockData.Count);

            IList <IWebElement> symbol_elements        = driver.FindElements(By.XPath("//*[@aria-label='Symbol']"));
            IList <IWebElement> lastPrice_elements     = driver.FindElements(By.XPath("//*[@aria-label='Last Price']"));
            IList <IWebElement> changePercent_elements = driver.FindElements(By.XPath("//*[@aria-label='Chg %']"));
            IList <IWebElement> volume_elements        = driver.FindElements(By.XPath("//*[@aria-label='Volume']"));
            IList <IWebElement> avgVolume_elements     = driver.FindElements(By.XPath("//*[@aria-label='Avg Vol (3m)']"));
            IList <IWebElement> marketCap_elements     = driver.FindElements(By.XPath("//*[@aria-label='Market Cap']"));

            ScrapedData scrape = new ScrapedData(symbol_elements, lastPrice_elements, changePercent_elements,
                                                 volume_elements, avgVolume_elements, marketCap_elements);

            //foreach (var item in name_elements)
            //{
            //    Console.WriteLine(item.GetAttribute("title"));
            //}

            ParseScrapedData(scrape);
            driver.Close();
        }
示例#2
0
        private static void ParseScrapedData(ScrapedData extractedData)
        {
            int stockTotal = extractedData.StockSymbols.Count;

            Console.WriteLine("stocktotal {0}", stockTotal);

            List <string> symbols       = new List <string>();
            List <double> lastPrice     = new List <double>();
            List <double> changePercent = new List <double>();
            List <string> volume        = new List <string>();
            List <string> avgVolume     = new List <string>();
            List <string> marketCap     = new List <string>();

            Stock stock = new Stock();

            for (int i = 0; i < stockTotal; i++)
            {
                symbols.Insert(i, Convert.ToString(extractedData.StockSymbols[i].Text));
                lastPrice.Insert(i, Convert.ToDouble(extractedData.StockLastPrices[i].Text));

                char trim = '%';
                changePercent.Insert(i, Convert.ToDouble(extractedData.StockChangePercents[i].Text.TrimEnd(trim)));

                volume.Insert(i, Convert.ToString(extractedData.StockVolumes[i].Text));
                avgVolume.Insert(i, Convert.ToString(extractedData.StockAvgVolumes[i].Text));
                marketCap.Insert(i, Convert.ToString(extractedData.StockMarketCaps[i].Text));

                stock = new Stock(symbols[i],
                                  lastPrice[i],
                                  changePercent[i],
                                  volume[i],
                                  avgVolume[i],
                                  marketCap[i]);

                Console.WriteLine("{0} stock created", symbols[i]);

                InsertStockHistory(stock);
                InsertCurrentStock(stock);
            }
        }