public ActionResult Index() { ViewBag.Message = "Welcome to ASP.NET MVC!"; //WebClient wc = new WebClient(); string pageData; //pageData = wc.DownloadString("http://www.fundamentus.com.br/resultado.php"); PostSubmitter post = new PostSubmitter(); post.Url = "http://www.fundamentus.com.br/resultado.php"; post.PostItems.Add("pl_min", "1"); post.PostItems.Add("pl_max", "20"); post.PostItems.Add("roe_min", "0"); post.PostItems.Add("pvp_min", "0"); post.PostItems.Add("pvp_max", "10"); post.PostItems.Add("liqcorr_min", "1"); post.PostItems.Add("margemebit_min", "0"); post.PostItems.Add("tx_cresc_rec_min", "0.08"); post.PostItems.Add("liq_min", "100000"); pageData = post.Post(); List<Stock> stocks = new List<Stock>(); HTMLSearchResult searcher = new HTMLSearchResult(); HTMLSearchResult result; int i = 0; for (i = 0; i < 20; i++ ) { result = searcher.GetTagData(pageData, "html", 1).GetTagData("body"). GetTagData("table").GetTagData("tr", i + 2).GetTagData("td").GetTagData("span").GetTagData("a"); Stock stock = new Stock { Name = result.TAGData }; result = searcher.GetTagData(pageData, "html", 1).GetTagData("body"). GetTagData("table").GetTagData("tr", i + 2).GetTagData("td", 3); stock.PL = Convert.ToDouble(result.TAGData); result = searcher.GetTagData(pageData, "html", 1).GetTagData("body"). GetTagData("table").GetTagData("tr", i + 2).GetTagData("td", 16); stock.ROE = Convert.ToDouble(result.TAGData.Split('%').First()); if (stock.PL > 1 && stock.PL < 20 && stock.ROE > 0) stocks.Add(stock); } stocks = stocks.OrderBy(a => a.PL).ToList(); i = 1; foreach (var stock in stocks) { stock.PLPosition = i; i++; } stocks = stocks.OrderByDescending(a => a.ROE).ToList(); i = 1; foreach (var stock in stocks) { stock.ROEPosition = i; stock.Position = stock.PLPosition + stock.ROEPosition; i++; } return View(stocks.OrderBy(a=>a.Position).ToList()); }
private HTMLSearchResult Result(string str) { HTMLSearchResult ret = new HTMLSearchResult(); ret.sSearchResult = str; return ret; }
//This function returns the html between given two search strings /// <summary> ///Extracts the Nth occurance of specified tag from the current HTML data. /// <param name="sFileData">The HTML page data, that contains the entire html page</param> /// <param name="sSearchTag">TAG to search</param> /// <param name="nOccurance">The Nth occurance to search for</param> /// <returns>Returns an HTMLSearchResult object containing the tag data.</returns> /// </summary> public HTMLSearchResult GetTagData(string sFileData, string sSearchTag, int nOccurance) { #region General_Variables_&_Validation string sTAGData = ""; int nStartPos = -1; int nEndPos = -1; TAGStack = new Stack<string>(); int nFoundOccurance = 0; // keep track of the no of instances we found, of the search tag. int nLen = sFileData.Length; int nLevel = 0; bool bFound = false; TAGStack.Clear(); if (nLen < 1) { throw (new ArgumentNullException("File Data cannot be null or blank string")); } if (sSearchTag.Length < 1) { throw (new ArgumentNullException("Search Tag cannot be null or blank string")); } if (nOccurance < 1) { throw (new ArgumentOutOfRangeException("The occurance number cannot be less than zero.")); } //--------------------START THE SEARCH----------------- #endregion try { nLen = nLen - sSearchTag.Length + 1; //the part where we can compare for (int i = 0; i < nLen; i++) { if (bFound == false) { if (sFileData[i] == '<' && sFileData[i + 1] != '/' && sFileData[i + 1] != '!') //found some tag... { i++; int nLastPos = i; sTAGAttribute = ""; //its class member. string sTAGName = ReadTillEndOfTag(sFileData, ref i, out sTAGAttribute); if (string.Compare(sSearchTag, sTAGName, true) == 0) { ShowPos(sSearchTag, nLevel, true); bFound = true; nStartPos = i+1;//Need to fix this... } } else if (sFileData[i] == '<' && sFileData[i + 1] == '/') //end tag found { string sEndTAG = ""; i += 2; while (sFileData[i] != '>') sEndTAG += sFileData[i++]; if (String.Compare(sEndTAG, sSearchTag, true) == 0 && nLevel == 0) { throw (new Exception("The Start tag was not found, however its end tag was found")); } } else { continue; } }//bFound==false else { if (sFileData[i] == '<' && sFileData[i + 1] != '/' && sFileData[i + 1] != '!') //found some tag... { i++; string sTagAttribute = ""; string sTAGName = ReadTillEndOfTag(sFileData, ref i, out sTagAttribute); if (String.Compare(sTAGName, "script", true) == 0) { int k =i+1; while (i < sFileData.Length) { string substr = sFileData.Substring(k, 9); if (String.Compare(substr, "</script>", true) == 0) { i = k+9; break; } k++; } } else if (String.Compare(sTAGName, "input", true) != 0 && String.Compare(sTAGName, "link", true) != 0 && String.Compare(sTAGName, "br", true) != 0 && String.Compare(sTAGName, "meta", true) != 0 && String.Compare(sTAGName, "img", true) != 0) { TAGStack.Push(sTAGName); nLevel++; ShowPos(sTAGName, nLevel, true); } } else if (sFileData[i] == '<' && sFileData[i + 1] == '/') //end tag found { int nLastCharPos = i - 1; string sEndTAG = ""; i += 2; while (sFileData[i] != '>') sEndTAG += sFileData[i++]; ShowPos(sEndTAG, nLevel, false); if (String.Compare(sEndTAG, sSearchTag, true) == 0 && nLevel == 0) { nFoundOccurance++; nEndPos = nLastCharPos; bFound = false; if (nFoundOccurance == nOccurance) { sTAGData = sFileData.Substring(nStartPos, nEndPos - nStartPos + 1); //Console.Write("\nSearch String\n======================================" + sTAGData + "\n=============================="); break; } //break; } else { string sPopedTag = TAGStack.Pop(); if (string.Compare(sPopedTag, sEndTAG, true) != 0) { //throw (new Exception("Unknown tag end")); TAGStack.Push(sPopedTag); nLevel++; } nLevel--; } } else { continue; } } } } catch (Exception e) { Console.Write(e.Message); } HTMLSearchResult result = new HTMLSearchResult(); result.sSearchResult = sTAGData; return result; }