BeautifulSoup.find_all () method does not work with tags with names

Today I came across very strange behavior when working with BeautifulSoup.

Let's look at a very simple html fragment:

<html><body><ix:nonfraction>lele</ix:nonfraction></body></html>

I am trying to get the contents of a tag <ix:nonfraction>using BeautifulSoup.

Everything works fine when using the method find:

from bs4 import BeautifulSoup

html = "<html><body><ix:nonfraction>lele</ix:nonfraction></body></html>"

soup = BeautifulSoup(html, 'lxml') # The parser used here does not matter

soup.find('ix:nonfraction')

>>> <ix:nonfraction>lele</ix:nonfraction>

However, trying to use the method find_all, I expect the list to be returned to this single element, which is not so!

soup.find_all('ix:nonfraction')
>>> []

In fact, find_allit seems to return an empty list every time a colon is present in the tag I'm looking for.

I was able to reproduce the problem on two different computers.

- , , ? find_all , , html.

+4
3

@yosemite_k , bs4 , . , . :

soup.find_all({"ix:nonfraction"})
soup.find_all('ix:nonfraction', limit=1)
soup.find_all('ix:nonfraction', text=True)

beautifulsoup, , , find find_all. , find find_all limit=1. _find_all :

if text is None and not limit and not attrs and not kwargs:

, :

# Optimization to find all tags with a given name.
if name.count(':') == 1:

, name:

# This is a name with a prefix.
prefix, name = name.split(':', 1)

, -. find_all , .

beautifulsoup4 == 4.6.0

def find(self, name=None, attrs={}, recursive=True, text=None,
         **kwargs):
    """Return only the first child of this Tag matching the given
    criteria."""
    r = None
    l = self.find_all(name, attrs, recursive, text, 1, **kwargs)
    if l:
        r = l[0]
    return r
findChild = find

def find_all(self, name=None, attrs={}, recursive=True, text=None,
             limit=None, **kwargs):
    """Extracts a list of Tag objects that match the given
    criteria.  You can specify the name of the Tag and any
    attributes you want the Tag to have.

    The value of a key-value pair in the 'attrs' map can be a
    string, a list of strings, a regular expression object, or a
    callable that takes a string and returns whether or not the
    string matches for some custom definition of 'matches'. The
    same is true of the tag name."""

    generator = self.descendants
    if not recursive:
        generator = self.children
    return self._find_all(name, attrs, text, limit, generator, **kwargs)


def _find_all(self, name, attrs, text, limit, generator, **kwargs):
    "Iterates over a generator looking for things that match."

    if text is None and 'string' in kwargs:
        text = kwargs['string']
        del kwargs['string']

    if isinstance(name, SoupStrainer):
        strainer = name
    else:
        strainer = SoupStrainer(name, attrs, text, **kwargs)

    if text is None and not limit and not attrs and not kwargs:
        if name is True or name is None:
            # Optimization to find all tags.
            result = (element for element in generator
                      if isinstance(element, Tag))
            return ResultSet(strainer, result)
        elif isinstance(name, str):
            # Optimization to find all tags with a given name.
            if name.count(':') == 1:
                # This is a name with a prefix.
                prefix, name = name.split(':', 1)
            else:
                prefix = None
            result = (element for element in generator
                      if isinstance(element, Tag)
                        and element.name == name
                      and (prefix is None or element.prefix == prefix)
            )
            return ResultSet(strainer, result)
    results = ResultSet(strainer)
    while True:
        try:
            i = next(generator)
        except StopIteration:
            break
        if i:
            found = strainer.search(i)
            if found:
                results.append(found)
                if limit and len(results) >= limit:
                    break
    return results
+3

ix .

soup.find_all({"ix:nonfraction"}) 

EDIT: "ix: nonfraction" , soup.find_all ( "ix: nonfraction" ) .

+2
>>> soup.findAll('ix:nonfraction')
[<ix:nonfraction>lele</ix:nonfraction>]

FindAll Documentation

-2
source

Source: https://habr.com/ru/post/1679822/


All Articles