Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用带lambda表达式的SoupStrainer_Python_Python 3.x_Beautifulsoup - Fatal编程技术网

Python 使用带lambda表达式的SoupStrainer

Python 使用带lambda表达式的SoupStrainer,python,python-3.x,beautifulsoup,Python,Python 3.x,Beautifulsoup,给定以下带有三个a标记的html: html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there

给定以下带有三个
a
标记的html:

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a class="sister" id="link1">Elsie</a>,
<a class="lister__item cf lister__item--upsell lister__item--has-ribbon brand-highlight" id="link2">Lacie</a> and
<a id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""
如何使用仅传递给
BeautifulSoup
对象的
parse\u
的SoupTrainer实例来模拟这一点?即

strainer = SoupStrainer(lambda tag: tag.name=='a' and 'lister__item' in tag.get('class'))
soup = BeautifulSoup(html_doc, 'html.parser', parse_only=strainer)
# TypeError: <lambda>() takes 1 positional argument but 2 were given
filter=SoupStrainer(lambda标记:tag.name==tag.get('class')中的'a'和'lister\u item')
soup=BeautifulSoup(html\u doc,'html.parser',parse\u only=filter)
#TypeError:()接受1个位置参数,但提供了2个

不知道具体做了什么,但通过一些诡计很容易找到解决方案。通过打印所有
*args
,首先查看传递给lambda可调用对象的内容:

strainer = bs4.SoupStrainer(lambda *args: print(args))
soup = bs4.BeautifulSoup(html_doc, 'html.parser', parse_only=strainer)
('html', {})
('head', {})
('title', {})
('body', {})
('p', {'class': 'title'})
('b', {})
('p', {'class': 'story'})
('a', {'class': 'sister', 'id': 'link1'})
('a', {'class': 'lister__item cf lister__item--upsell lister__item--has-ribbon brand-highlight', 'id': 'link2'})
('a', {'class': 'sister', 'id': 'link3'})
('p', {'class': 'story'})
现在,使用您的
lambda
(其中一个是
BeautifulSoup
类型,另一个是being
元素。Tag

strainer = bs4.SoupStrainer(lambda tag_name, d: tag_name == 'a' and 'lister__item' in d['class'])
soup = bs4.BeautifulSoup(html_doc, 'html.parser', parse_only=strainer)
soup
现在包含相同的结果:

>>> soup
<a class="lister__item cf lister__item--upsell lister__item--has-ribbon brand-highlight" id="link2">Lacie</a>
>>汤
莱西

不知道具体做了什么,但通过一些技巧很容易得到解决方案。通过打印所有的
*args
,首先查看传递给lambda callable的内容:

strainer = bs4.SoupStrainer(lambda *args: print(args))
soup = bs4.BeautifulSoup(html_doc, 'html.parser', parse_only=strainer)
('html', {})
('head', {})
('title', {})
('body', {})
('p', {'class': 'title'})
('b', {})
('p', {'class': 'story'})
('a', {'class': 'sister', 'id': 'link1'})
('a', {'class': 'lister__item cf lister__item--upsell lister__item--has-ribbon brand-highlight', 'id': 'link2'})
('a', {'class': 'sister', 'id': 'link3'})
('p', {'class': 'story'})
现在,使用您的
lambda
(其中一个是
BeautifulSoup
类型,另一个是being
元素。Tag

strainer = bs4.SoupStrainer(lambda tag_name, d: tag_name == 'a' and 'lister__item' in d['class'])
soup = bs4.BeautifulSoup(html_doc, 'html.parser', parse_only=strainer)
soup
现在包含相同的结果:

>>> soup
<a class="lister__item cf lister__item--upsell lister__item--has-ribbon brand-highlight" id="link2">Lacie</a>
>>汤
莱西

我应该注意的一个问题。不是我所有的
a
标记都有
class
属性,所以我得到了一个KeyError。现在在lambda中玩
hasattr
,但是你的答案看起来不错。更新了问题以反映。@BradSolomon而不是
hasattr
使用
d.get('class',[])
同样的效果。我应该注意的一个问题。不是我的
a
标记都有
class
属性,所以我得到了一个KeyError。现在在lambda中玩
hasattr
,但是你的答案看起来不错。更新了问题以反映。@bradsomon而不是
hasattr
使用
d('class',[])
以获得相同的效果。