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',[])
以获得相同的效果。