public BaseCrawlerAsyncService(string stock_id, CrawlerURL type) { switch (type) { case CrawlerURL.StockPriceNow: URL = stockURL + stock_id; mainSelector = @"center > table:nth-child(9) > tbody > tr > td > table > tbody > tr:nth-child(2) > "; selector = new StockSelector(); break; case CrawlerURL.StockJuristic: URL = JuristicURL + stock_id + ".htm"; mainSelector = @"#main3 > div.mbx.bd3 > div.tabvl > table > tbody > "; //selector = new StockSelector(); break; case CrawlerURL.GainDrop5Day: URL = stock5DayGainDrop + stock_id + ".htm"; mainSelector = @"#main3 > div.mbx.bd3 > div.tab > table > tbody > "; //selector = new StockSelector(); break; case CrawlerURL.Yields5Year: URL = stockYieldsURL(stock_id); mainSelector = @"#mainCol > div.br-trl > table > tbody > "; //selector = new StockSelector(); break; } }
public async Task <StockNowPriceViewModel> GetStockPriceNowAsync(string stock_id) { StockNowPriceViewModel vm = new StockNowPriceViewModel(); string URL = stockURL + stock_id; var dom = await BrowsingContext.New(config).OpenAsync(URL);// .Result; var mainSelector = @"center > table:nth-child(9) > tbody > tr > td > table > tbody > tr:nth-child(2) > "; StockSelector stock = new StockSelector() { StockDeal = mainSelector + "td:nth-child(3)", StockGainDrop = mainSelector + "td:nth-child(6) > font", }; vm.Price = dom.QuerySelector(stock.StockDeal).TextContent ?? ""; vm.GainDrop = dom.QuerySelector(stock.StockGainDrop).TextContent ?? ""; if (!string.IsNullOrWhiteSpace(vm.GainDrop)) { vm.GainDrop = vm.GainDrop.Replace("\n", "").Trim(); } if (vm.GainDrop.Contains("△")) { vm.Color = "Red"; } else if (vm.GainDrop.Contains("▽")) { vm.Color = "Green"; } else { vm.Color = "Balck"; } return(vm); }
public StockViewModel GetStockById(int id) { var URL_Id = id.ToString().PadLeft(4, '0'); StockViewModel stockVM = new StockViewModel() { StockID = URL_Id }; string URL = "q/q?s=" + URL_Id; var dom = BrowsingContext.New(config).OpenAsync(yahooStockURL + URL).Result; #region Hide var mainSelector = @"center > table:nth-child(9) > tbody > tr > td > table > tbody > tr:nth-child(2) > "; StockSelector stock = new StockSelector() { StockName = mainSelector + "td:nth-child(1) > a:nth-child(1)", StockTime = mainSelector + "td:nth-child(2)", StockDeal = mainSelector + "td:nth-child(3)", StockBuy = mainSelector + "td:nth-child(4)", StockSell = mainSelector + "td:nth-child(5)", StockGainDrop = mainSelector + "td:nth-child(6) > font", StockShareCount = mainSelector + "td:nth-child(7)", StockYesterday = mainSelector + "td:nth-child(8)", StockOpen = mainSelector + "td:nth-child(9)", StockTop = mainSelector + "td:nth-child(10)", StockDown = mainSelector + "td:nth-child(11)", }; //判斷是否有此股票 var page = dom.QuerySelector(stock.StockName); if (page == null) { return(null); } var newStockProp = (from x in TypeDescriptor.GetProperties(stock).Cast <PropertyDescriptor>() where x.Name != "StockID" select x); var setStockProp = (from x in TypeDescriptor.GetProperties(stockVM).Cast <PropertyDescriptor>() where x.Name != "StockID" select x); foreach (var prop in newStockProp) { var selectorString = prop.GetValue(stock).ToString(); var data = (Object)dom.QuerySelectorAll(selectorString) .Select(x => x.TextContent).FirstOrDefault().Replace("\n", "").Replace(URL_Id, ""); foreach (var item in setStockProp) { if (prop.Name == item.Name) { if (item.Name == "StockGainDrop") { data = (Object)data.ToString(); } PropertyInfo p = stockVM.GetType().GetProperty(item.Name); p.SetValue(stockVM, data); } } } #endregion return(stockVM); }