【发布时间】:2020-06-18 17:16:24
【问题描述】:
我目前正在做我的课堂作业。我必须从这个网页的 SPECS 表中提取数据。
我需要的数据存储为
<h2 class="crux-product-title">Specs</h2>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="product-model-features-specs-item">
<div class="row">
<div class='col-lg-6 col-md-6 col-sm-6 col-xs-12 product-model-features-specs-item-key'>
<span class="crux-body-copy crux-body-copy--small--bold">
Programmable
<span class="product-model-tooltip">
<span class="crux-icons crux-icons-help-information" aria-hidden="true"></span>
<span class="product-model-tooltip-window">
<span class="crux-icons crux-icons-close" aria-hidden="true"></span>
<span class="crux-body-copy crux-body-copy--small--bold">Programmable</span>
<span class="crux-body-copy crux-body-copy--small">Programmable models have a clock and can be set to brew at a specified time.</span>
</span>
</span>
</span>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 product-model-features-specs-item-value">
<span class='crux-body-copy crux-body-copy--small'>Yes</span>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="product-model-features-specs-item">
<div class="row">
<div class='col-lg-6 col-md-6 col-sm-6 col-xs-12 product-model-features-specs-item-key'>
<span class="crux-body-copy crux-body-copy--small--bold">
Thermal carafe/mug
<span class="product-model-tooltip">
<span class="crux-icons crux-icons-help-information" aria-hidden="true"></span>
<span class="product-model-tooltip-window">
<span class="crux-icons crux-icons-close" aria-hidden="true"></span>
<span class="crux-body-copy crux-body-copy--small--bold">Thermal carafe/mug</span>
<span class="crux-body-copy crux-body-copy--small">Keeps coffee warm for about four hours; thermal mugs don't hold heat as well.</span>
</span>
</span>
</span>
我需要为三个跨度类创建列表
class="crux-body-copy crux-body-copy--small--bold
crux-body-copy crux-body-copy--small
crux-body-copy crux-body-copy--small
提取表格的问题是因为表格中使用了多个跨度。
我用BEAUTIFUL SOUP,用find_all和find,用span名字来调用它。
我总是得到第一个值。
我该怎么做?
【问题讨论】:
-
到目前为止我们能看到你的代码吗?通常,如果您有一个元素列表 myelements[2] 例如,您将不得不指定一个索引。
-
'url = "consumerreports.org/products/drip-coffee-maker/…" html_content = requests.get(url).text soup = BeautifulSoup(html_content, "lxml") print(soup.prettify()) table = soup.find_all( "span", attrs={"class": "crux-body-copy crux-body-copy--small"}) print(table)'
标签: python html web-scraping html-table beautifulsoup