Пример #1
0
        /// <summary>
        /// Try parse tag with data you need
        /// </summary>
        /// <param name="page"Html page to parse></param>
        /// <param name="dataName">data(time,weather,calories of food,etc)</param>
        /// <param name="debug">turn true to show process</param>
        /// <returns>THE DATA!</returns>
        public static string TryParseInfoTag(string page, string dataName, IHtmlTagToFind tagToFind, bool debug = false)
        {
            var data = "";

            if (page.Contains(tagToFind.TagId))
            {
                try
                {
                    if (debug)
                    {
                        Console.WriteLine($"Trying to get {dataName}...");
                    }
                    if (tagToFind.OwnTag)
                    {
                        data = GetInnerText(page, tagToFind);
                    }
                    else
                    {
                        data = GetInnerTextWithoutSelfId(page, tagToFind);
                    }
                    if (debug)
                    {
                        Console.WriteLine($"Got {dataName}!");
                    }
                    return(data);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Cant parse {dataName} for some reason!");
                }
            }
            Console.WriteLine($"There is no {dataName}!");
            return("");
        }
Пример #2
0
        /// <summary>
        /// Get inner text of td in table(the later td is in the table,the more lengthOfText needs to be)
        /// </summary>
        /// <returns>inner text of td</returns>
        public static string GetInnerTextFromTables(string page, IHtmlTagToFind tagToFind)
        {
            var infoBlock       = page.Substring(page.IndexOf(tagToFind.TagId, StringComparison.InvariantCultureIgnoreCase), tagToFind.LengthSearch);
            var withoutCloseTag = infoBlock.Substring(0, infoBlock.LastIndexOf(tagToFind.ClosingTag, StringComparison.InvariantCultureIgnoreCase));
            var withoutOpenTag  = withoutCloseTag.Substring(withoutCloseTag.LastIndexOf(">", StringComparison.InvariantCultureIgnoreCase) + 1);
            var text            = withoutOpenTag.Replace("&nbsp;", "").Replace("&#39;", "").Replace(".", "").Trim();

            return(text);
        }