예제 #1
0
 def _parse_match(self, match):
     """
     If there is a group dict, return the dict;
         even if there's only one value in the dict, return a dictionary;
     If there is a group in match, return the group;
         if there is only one value in the group, return the value;
     if there has no group, return the whole matched string;
     if there are many groups, return a tuple;
     :param match:
     :return:
     """
     if not match:
         if self.default:
             return self.default
         else:
             raise NothingMatchedError(
                 f"Extract `{self._re_select}` error, "
                 f"please check selector or set parameter named `default`"
             )
     else:
         string = match.group()
         groups = match.groups()
         group_dict = match.groupdict()
         if group_dict:
             return group_dict
         if groups:
             return groups[0] if len(groups) == 1 else groups
         return string
예제 #2
0
    def extract(self, html_etree: etree._Element, is_source: bool = False):
        elements = self._get_elements(html_etree=html_etree)

        if is_source:
            return elements if self.many else elements[0]

        if elements:
            results = [self._parse_element(element) for element in elements]
        elif self.default is None:
            raise NothingMatchedError(
                f"Extract `{self.css_select or self.xpath_select}` error, "
                f"please check selector or set parameter named `default`")
        else:
            results = [self.default]

        return results if self.many else results[0]