コード例 #1
0
        private string GetSelectorValue(Page page, Selector selectorAttribute)
        {
            string result   = string.Empty;
            var    selector = selectorAttribute.ToSelector();

            if (selectorAttribute.Type == SelectorType.Enviroment)
            {
                if (selector is EnviromentSelector enviromentSelector)
                {
                    result = SelectorUtil.GetEnviromentValue(enviromentSelector.Field, page, 0)?.ToString();
                }
            }
            else
            {
                result = page.Selectable.Select(selector).GetValue();
            }

            if (!string.IsNullOrEmpty(result) && TotalPageFormatters != null)
            {
                foreach (var formatter in TotalPageFormatters)
                {
                    result = formatter.Formate(result)?.ToString();
                }
            }

            if (string.IsNullOrWhiteSpace(result))
            {
                throw new SpiderException("The result of total selector is null");
            }
            else
            {
                return(result);
            }
        }
コード例 #2
0
ファイル: ModelExtractor.cs プロジェクト: xyfy/DotnetSpider
        private string ExtractField(ISelectable item, Page page, Field field, int index)
        {
            if (field == null)
            {
                return(null);
            }

            var selector = field.ToSelector();

            if (selector == null)
            {
                return(null);
            }

            object value;

            if (selector is EnviromentSelector)
            {
                var enviromentSelector = selector as EnviromentSelector;
                value = SelectorUtil.GetEnviromentValue(enviromentSelector.Field, page, index);
            }
            else
            {
                value = field.Option == FieldOptions.Count
                                        ? item.SelectList(selector).Nodes().Count().ToString()
                                        : item.Select(selector)?.GetValue(ConvertToValueOption(field.Option));
            }

            if (field.Formatters != null && field.Formatters.Count() > 0)
            {
                foreach (var formatter in field.Formatters)
                {
#if DEBUG
                    try
                    {
#endif
                    value = formatter.Formate(value);
#if DEBUG
                }
                catch (Exception e)
                {
                    Log.Logger.Error(e.ToString());
                }
#endif
                }
            }

            return(value?.ToString());
        }
コード例 #3
0
        private object ExtractField(ISelectable item, Page page, Column field, int index)
        {
            if (field == null)
            {
                return(null);
            }
            ISelector selector = SelectorUtil.Parse(field.Selector);

            if (selector == null)
            {
                return(null);
            }

            if (selector is EnviromentSelector)
            {
                var enviromentSelector = selector as EnviromentSelector;
                var value = SelectorUtil.GetEnviromentValue(enviromentSelector.Field, page, index);
                foreach (var formatter in field.Formatters)
                {
#if DEBUG
                    try
                    {
#endif
                    value = formatter.Formate(value);
#if DEBUG
                }
                catch (Exception e)
                {
                    Logger.NLog(e.ToString(), Level.Error);
                }
#endif
                }
                return(TryConvert(value, field.DataType));
            }
            else
            {
                bool needCount = field.Option == PropertyDefine.Options.Count;
                if (needCount)
                {
                    var values = item.SelectList(selector).Nodes();
                    return(values.Count);
                }
                else
                {
                    var value = (object)item.Select(selector)?.GetValue(field.Option == PropertyDefine.Options.PlainText);

                    foreach (var formatter in field.Formatters)
                    {
#if DEBUG
                        try
                        {
#endif
                        value = formatter.Formate(value);
#if DEBUG
                    }
                    catch (Exception e)
                    {
                        Logger.NLog(e.ToString(), Level.Error);
                    }
#endif
                    }

                    return(TryConvert(value, field.DataType));
                }
            }
        }