gpt4 book ai didi

python - 忽略工具提示文本并仅获取当前使用 Beautiful Soup 显示的文本

转载 作者:行者123 更新时间:2023-12-01 08:22:57 24 4
gpt4 key购买 nike

我试图仅从列表中获取显示文本,但我不断取回链接的文本工具提示。

我怎样才能改变这个,以便我得到

'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 &gt; 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 &gt; 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 &gt; 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 &gt; 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)

最佳答案

您似乎只需要 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 &gt; 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 &gt; 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 &gt; 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 &gt; 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

关于python - 忽略工具提示文本并仅获取当前使用 Beautiful Soup 显示的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54505324/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com