BeautifulSoup.find_all()方法不使用命名空间标记

时间:2017-06-21 15:25:36

标签: python python-3.x beautifulsoup bs4

我今天在使用BeautifulSoup时遇到了一种非常奇怪的行为。

让我们看看一个非常简单的html片段:

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

我正在尝试使用BeautifulSoup获取<ix:nonfraction>标记的内容。

使用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>

但是,在尝试使用find_all方法时,我希望返回一个包含此单个元素的列表,但事实并非如此!

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

事实上,每当我正在搜索的标签中出现冒号时,find_all似乎都会返回一个空列表。

我已经能够在两台不同的计算机上重现这个问题。

有没有人有解释,更重要的是,有一个解决方法? 我需要使用find_all方法,因为我的实际案例要求我在整个html页面上获取所有这些标签。

3 个答案:

答案 0 :(得分:7)

@ yosemite_k解决方案的工作原因是因为在bs4的源代码中,它正在跳过导致此行为的特定条件。事实上,你可以做很多变化,这会产生同样的结果。例子:

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

以下是beautifulsoup源代码中的一个片段,其中显示了致电findfind_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

答案 1 :(得分:3)

将标记名留空,并使用ix作为属性。

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

运作良好

编辑:'ix:nonfraction'不是标签名称,因此soup.find_all(“ix:nonfraction”)返回一个不存在标签的空列表。

答案 2 :(得分:-2)

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

FindAll Documentation