Пример #1
0
        /// <summary>
        /// 把BaseSelector转换成真正的查询器
        /// </summary>
        /// <param name="selector">BaseSelector</param>
        /// <returns>查询器</returns>
        public static ISelector ToSelector(this Selector selector)
        {
            if (selector != null)
            {
                string expression = selector.Expression;

                switch (selector.Type)
                {
                case SelectorType.Css:
                {
                    NotNullExpression(selector);
                    return(Selectors.Css(expression));
                }

                case SelectorType.Enviroment:
                {
                    return(Selectors.Enviroment(expression));
                }

                case SelectorType.JsonPath:
                {
                    NotNullExpression(selector);
                    return(Selectors.JsonPath(expression));
                }

                case SelectorType.Regex:
                {
                    NotNullExpression(selector);
                    if (string.IsNullOrEmpty(selector.Arguments))
                    {
                        return(Selectors.Regex(expression));
                    }
                    else
                    {
                        int group;
                        if (int.TryParse(selector.Arguments, out group))
                        {
                            return(Selectors.Regex(expression, group));
                        }
                        throw new SpiderException("Regex argument should be a number set to group: " + selector);
                    }
                }

                case SelectorType.XPath:
                {
                    NotNullExpression(selector);
                    return(Selectors.XPath(expression));
                }

                default:
                {
                    throw new SpiderException($"Selector {selector} unsupoort");
                }
                }
            }
            else
            {
                return(null);
            }
        }
Пример #2
0
        public static ISelector Parse(BaseSelector selector)
        {
            if (string.IsNullOrEmpty(selector?.Expression))
            {
                return(null);
            }

            string expression = selector.Expression;

            switch (selector.Type)
            {
            case SelectorType.Css:
            {
                return(Selectors.Css(expression));
            }

            case SelectorType.Enviroment:
            {
                return(Selectors.Enviroment(expression));
            }

            case SelectorType.JsonPath:
            {
                return(Selectors.JsonPath(expression));
            }

            case SelectorType.Regex:
            {
                if (string.IsNullOrEmpty(selector.Argument))
                {
                    return(Selectors.Regex(expression));
                }
                else
                {
                    int group;
                    if (int.TryParse(selector.Argument, out group))
                    {
                        return(Selectors.Regex(expression, group));
                    }
                    throw new SpiderException("Regex argument should be a number set to group: " + selector);
                }
            }

            case SelectorType.XPath:
            {
                return(Selectors.XPath(expression));
            }
            }
            throw new SpiderException("Not support selector: " + selector);
        }
Пример #3
0
        public static ISelector Parse(Selector selector)
        {
            if (string.IsNullOrEmpty(selector?.Expression))
            {
                return(null);
            }

            string expression = selector.Expression;

            switch (selector.Type)
            {
            case SelectorType.Css:
            {
                return(Selectors.Css(expression));
            }

            case SelectorType.Enviroment:
            {
                return(Selectors.Enviroment(expression));
            }

            case SelectorType.JsonPath:
            {
                return(Selectors.JsonPath(expression));
            }

            case SelectorType.Regex:
            {
                return(Selectors.Regex(expression));
            }

            case SelectorType.XPath:
            {
                return(Selectors.XPath(expression));
            }
            }
            throw new SpiderException("Not support selector: " + selector);
        }