Exemplo n.º 1
0
 public void SetExtractionLocation(string location, ExtractionLocation.ExtractionLocationTypes locationType, bool includeChildNodes)
 {
     if (location != null)
     {
         Location = new ExtractionLocation(
             location,
             locationType,
             includeChildNodes
             );
     }
 }
        protected override IEnumerable <(string ExtractedValue, ResponseParserPositionPointer ExtractedValuePosition)> ExtractItemValuesFromLocation(ExtractionLocation location, ResponseParserPositionPointer?relativeLocationBase = null)
        {
            var xPathNavigator = _xPathNavigator;

            if (relativeLocationBase != null)
            {
                var nodeSelector = XPathSelectorAttributeRegex.Replace(relativeLocationBase.Value.Location.Selector, string.Empty);
                xPathNavigator = _xPathNavigator.Select("(" + nodeSelector + ")[" + (relativeLocationBase.Value.ElementIndex + 1) + "]")
                                 .Cast <XPathNavigator>()
                                 .Single();
            }

            int extractedValueIndex = 0;

            foreach (var value in xPathNavigator.Select(location.Selector)
                     .Cast <HtmlNodeNavigator>()
                     .Select(nav => GetNodeValue(nav, location.LocationType, location.IncludeChildNodes))
                     )
            {
                yield return(value?.Trim(), new ResponseParserPositionPointer(location, extractedValueIndex++));
            }
        }
        protected override IEnumerable <(string ExtractedValue, ResponseParserPositionPointer ExtractedValuePosition)> ExtractItemValuesFromLocation(ExtractionLocation location, ResponseParserPositionPointer?relativeLocationBase = null)
        {
            var selector = location.Selector;

            if (relativeLocationBase != null)
            {
                selector = $"{relativeLocationBase.Value.Location.Selector}[{relativeLocationBase.Value.ElementIndex + 1}] {location.Selector}";
            }

            int extractedValueIndex = 0;

            foreach (var value in _htmlDocument.QuerySelectorAll(selector)
                     .Select(node => GetNodeValue(node, location.LocationType, location.IncludeChildNodes))
                     )
            {
                yield return(value?.Trim(), new ResponseParserPositionPointer(location, extractedValueIndex++));
            }
        }