Recall that all the search methods take more or less the same arguments. Behind the scenes, your arguments
to a search method get transformed into a SoupStrainer
object. If
you call one of the methods that returns a list (like findAll
), the SoupStrainer
object is made available as the source
property of
the resulting list.
回忆起所有的搜索方法都是或多或少使用了一些一样的参数。
在后台,你传递给搜索函数的参数都会传给SoupStrainer
对象。
如果你所使用的函数返回一个list(如findAll
),那是SoupStrainer
对象使
结果列表的source
属性变的可用。
from BeautifulSoup import BeautifulStoneSoup xml = '<person name="Bob"><parent rel="mother" name="Alice">' xmlSoup = BeautifulStoneSoup(xml) results = xmlSoup.findAll(rel='mother') results.source # <BeautifulSoup.SoupStrainer instance at 0xb7e0158c> str(results.source) # "None|{'rel': 'mother'}"
The SoupStrainer
constructor takes most of the same arguments as find
: name
, attrs
, text
, and **kwargs
. You can pass
in a SoupStrainer
as the name
argument to any search method:
SoupStrainer
的构造器几乎使用和find
一样的参数: name
, attrs
, text
,
和**kwargs
.
你可以在一个SoupStrainer
中传递和其他搜索方法一样的name参数:
xmlSoup.findAll(results.source) == results # True customStrainer = BeautifulSoup.SoupStrainer(rel='mother') xmlSoup.findAll(customStrainer) == results # True
Yeah, who cares, right? You can carry around a method call's
arguments in many other ways. But another thing you can do with SoupStrainer
is pass it into the soup constructor to restrict the
parts of the document that actually get parsed. That brings us to the
next section:
耶,谁会在意,对不对?你可以把一个方法的参数用在很多其他地方。
还有一件你可以用SoupStrainer
做的事是,将它传递给soup的构建器,来部分的解析文档。
下一节,我们就谈这个。