我试图仅从列表中获取显示的文本,但是我一直在找回链接文本的工具提示。
如何更改此设置,以便获得
'1/2 lemon, zested'
代替
'½ lemon, zested Lemon le-monOval in shape, with a pronouced bulge on one end, lemons are one of the most versatile fruits...'
html摘录为:
<div class="ingredients-list__content"><ul class="ingredients-list__group"><li class="ingredients-list__item" content="225g softened butter" itemprop="ingredients">225g softened <a class="ingredients-list__glossary-link" data-tooltip-content="#ingredients-glossary > article" data-tooltip-flyout="true" data-tooltip-hide-delay="200" data-tooltip-width="350" href="/glossary/butter">butter</a><span class="ingredients-list__glossary-element" id="ingredients-glossary"><article class="node node-glossary-item node-teaser node-teaser clearfix main row grid-padding" id="node-261201" role="main"><div class="node-image"> <a href="/glossary/butter"><img alt="Butter" height="100" src="//www.bbcgoodfood.com/sites/default/files/styles/bbcgf_thumbnail_search/public/glossary/butter.jpg?itok=WmVkxpe0" title="Butter" width="100"/></a></div><h2 class="node-title node-glossary-title"><a href="/glossary/butter">Butter</a></h2> <span class="fonetic text-style-alt">butt-err</span><p>Butter is made when lactic-acid producing bacteria are added to cream and churned to make an…</p> </article></span></li><li class="ingredients-list__item" content="225g golden caster sugar" itemprop="ingredients">225g golden caster sugar</li><li class="ingredients-list__item" content="4 large eggs" itemprop="ingredients">4 large <a class="ingredients-list__glossary-link" data-tooltip-content="#ingredients-glossary--2 > article" data-tooltip-flyout="true" data-tooltip-hide-delay="200" data-tooltip-width="350" href="/glossary/egg">eggs</a><span class="ingredients-list__glossary-element" id="ingredients-glossary--2"><article class="node node-glossary-item node-teaser node-teaser clearfix main row grid-padding" id="node-252381" role="main"><div class="node-image"> <a href="/glossary/egg"><img alt="Eggs" height="100" src="//www.bbcgoodfood.com/sites/default/files/styles/bbcgf_thumbnail_search/public/glossary/eggs.jpg?itok=_1Q-Z6FA" title="The ultimate convenience food, eggs are powerhouses of nutrition, packed with protein and a range of 18 vitamins and minerals" width="100"/></a></div><h2 class="node-title node-glossary-title"><a href="/glossary/egg">Egg</a></h2> <span class="fonetic text-style-alt">egg</span><p>The ultimate convenience food, eggs are powerhouses of nutrition, packed with protein and a…</p> </article></span></li><li class="ingredients-list__item" content="½ lemon, zested" itemprop="ingredients">½ <a class="ingredients-list__glossary-link" data-tooltip-content="#ingredients-glossary--3 > article" data-tooltip-flyout="true" data-tooltip-hide-delay="200" data-tooltip-width="350" href="/glossary/lemon">lemon</a>, zested<span class="ingredients-list__glossary-element" id="ingredients-glossary--3"><article class="node node-glossary-item node-teaser node-teaser clearfix main row grid-padding" id="node-259661" role="main"><div class="node-image"> <a href="/glossary/lemon"><img alt="Lemon" height="100" src="//www.bbcgoodfood.com/sites/default/files/styles/bbcgf_thumbnail_search/public/glossary/lemon.jpg?itok=fstxl0E6" title="Lemons are one of the most versatile fruits around, and contain a high level of Vitamin C" width="100"/></a></div><h2 class="node-title node-glossary-title"><a href="/glossary/lemon">Lemon</a></h2> <span class="fonetic text-style-alt">le-mon</span><p>Oval in shape, with a pronouced bulge on one end, lemons are one of the most versatile fruits…</p> </article></span></li><li class="ingredients-list__item" content="1 tsp vanilla extract" itemprop="ingredients">1 tsp vanilla extract</li><li class="ingredients-list__item" content="225g self-raising flour" itemprop="ingredients">225g self-raising flour</li><li class="ingredients-list__item" content="splash of milk" itemprop="ingredients">splash of <a class="ingredients-list__glossary-link" data-tooltip-content="#ingredients-glossary--4 > article" data-tooltip-flyout="true" data-tooltip-hide-delay="200" data-tooltip-width="350" href="/glossary/milk">milk</a><span class="ingredients-list__glossary-element" id="ingredients-glossary--4"><article class="node node-glossary-item node-teaser node-teaser clearfix main row grid-padding" id="node-260691" role="main"><div class="node-image"> <a href="/glossary/milk"><img alt="" height="100" src="//www.bbcgoodfood.com/sites/default/files/styles/bbcgf_thumbnail_search/public/glossary/milk-resized.jpg?itok=z-tC0wLi" width="100"/></a></div><h2 class="node-title node-glossary-title"><a href="/glossary/milk">Milk</a></h2> <span class="fonetic text-style-alt">mill-k</span><p>One of the most widely used ingredients, milk is often referred to as a complete food. While cow…</p> </article></span></li><li class="ingredients-list__item" content="Optional fillings of lemon curd, jam, lightly whipped cream" itemprop="ingredients">Optional fillings of lemon curd, jam, lightly whipped cream</li><li class="ingredients-list__item" content="icing sugar for dusting" itemprop="ingredients">icing sugar for dusting</li></ul></div>
有什么想法吗?
ingredients = ingredientsBlock.findAll('li', class_='ingredients-list__item')
print(ingredients[3].text)
答案 0 :(得分:2)
您似乎只想要content
属性。可以这样完成:
from bs4 import BeautifulSoup
html = """<div class="ingredients-list__content"><ul class="ingredients-list__group"><li class="ingredients-list__item" content="225g softened butter" itemprop="ingredients">225g softened <a class="ingredients-list__glossary-link" data-tooltip-content="#ingredients-glossary > article" data-tooltip-flyout="true" data-tooltip-hide-delay="200" data-tooltip-width="350" href="/glossary/butter">butter</a><span class="ingredients-list__glossary-element" id="ingredients-glossary"><article class="node node-glossary-item node-teaser node-teaser clearfix main row grid-padding" id="node-261201" role="main"><div class="node-image"> <a href="/glossary/butter"><img alt="Butter" height="100" src="//www.bbcgoodfood.com/sites/default/files/styles/bbcgf_thumbnail_search/public/glossary/butter.jpg?itok=WmVkxpe0" title="Butter" width="100"/></a></div><h2 class="node-title node-glossary-title"><a href="/glossary/butter">Butter</a></h2> <span class="fonetic text-style-alt">butt-err</span><p>Butter is made when lactic-acid producing bacteria are added to cream and churned to make an…</p> </article></span></li><li class="ingredients-list__item" content="225g golden caster sugar" itemprop="ingredients">225g golden caster sugar</li><li class="ingredients-list__item" content="4 large eggs" itemprop="ingredients">4 large <a class="ingredients-list__glossary-link" data-tooltip-content="#ingredients-glossary--2 > article" data-tooltip-flyout="true" data-tooltip-hide-delay="200" data-tooltip-width="350" href="/glossary/egg">eggs</a><span class="ingredients-list__glossary-element" id="ingredients-glossary--2"><article class="node node-glossary-item node-teaser node-teaser clearfix main row grid-padding" id="node-252381" role="main"><div class="node-image"> <a href="/glossary/egg"><img alt="Eggs" height="100" src="//www.bbcgoodfood.com/sites/default/files/styles/bbcgf_thumbnail_search/public/glossary/eggs.jpg?itok=_1Q-Z6FA" title="The ultimate convenience food, eggs are powerhouses of nutrition, packed with protein and a range of 18 vitamins and minerals" width="100"/></a></div><h2 class="node-title node-glossary-title"><a href="/glossary/egg">Egg</a></h2> <span class="fonetic text-style-alt">egg</span><p>The ultimate convenience food, eggs are powerhouses of nutrition, packed with protein and a…</p> </article></span></li><li class="ingredients-list__item" content="½ lemon, zested" itemprop="ingredients">½ <a class="ingredients-list__glossary-link" data-tooltip-content="#ingredients-glossary--3 > article" data-tooltip-flyout="true" data-tooltip-hide-delay="200" data-tooltip-width="350" href="/glossary/lemon">lemon</a>, zested<span class="ingredients-list__glossary-element" id="ingredients-glossary--3"><article class="node node-glossary-item node-teaser node-teaser clearfix main row grid-padding" id="node-259661" role="main"><div class="node-image"> <a href="/glossary/lemon"><img alt="Lemon" height="100" src="//www.bbcgoodfood.com/sites/default/files/styles/bbcgf_thumbnail_search/public/glossary/lemon.jpg?itok=fstxl0E6" title="Lemons are one of the most versatile fruits around, and contain a high level of Vitamin C" width="100"/></a></div><h2 class="node-title node-glossary-title"><a href="/glossary/lemon">Lemon</a></h2> <span class="fonetic text-style-alt">le-mon</span><p>Oval in shape, with a pronouced bulge on one end, lemons are one of the most versatile fruits…</p> </article></span></li><li class="ingredients-list__item" content="1 tsp vanilla extract" itemprop="ingredients">1 tsp vanilla extract</li><li class="ingredients-list__item" content="225g self-raising flour" itemprop="ingredients">225g self-raising flour</li><li class="ingredients-list__item" content="splash of milk" itemprop="ingredients">splash of <a class="ingredients-list__glossary-link" data-tooltip-content="#ingredients-glossary--4 > article" data-tooltip-flyout="true" data-tooltip-hide-delay="200" data-tooltip-width="350" href="/glossary/milk">milk</a><span class="ingredients-list__glossary-element" id="ingredients-glossary--4"><article class="node node-glossary-item node-teaser node-teaser clearfix main row grid-padding" id="node-260691" role="main"><div class="node-image"> <a href="/glossary/milk"><img alt="" height="100" src="//www.bbcgoodfood.com/sites/default/files/styles/bbcgf_thumbnail_search/public/glossary/milk-resized.jpg?itok=z-tC0wLi" width="100"/></a></div><h2 class="node-title node-glossary-title"><a href="/glossary/milk">Milk</a></h2> <span class="fonetic text-style-alt">mill-k</span><p>One of the most widely used ingredients, milk is often referred to as a complete food. While cow…</p> </article></span></li><li class="ingredients-list__item" content="Optional fillings of lemon curd, jam, lightly whipped cream" itemprop="ingredients">Optional fillings of lemon curd, jam, lightly whipped cream</li><li class="ingredients-list__item" content="icing sugar for dusting" itemprop="ingredients">icing sugar for dusting</li></ul></div>"""
soup = BeautifulSoup(html, "html.parser")
ingredientsBlock = soup.find('div', class_='ingredients-list__content')
for ingredient in ingredientsBlock.find_all('li', class_='ingredients-list__item'):
print(ingredient['content'])
然后将成分显示为:
225g softened butter
225g golden caster sugar
4 large eggs
½ lemon, zested
1 tsp vanilla extract
225g self-raising flour
splash of milk
Optional fillings of lemon curd, jam, lightly whipped cream
icing sugar for dusting