【发布时间】:2021-12-11 14:28:24
【问题描述】:
我正在尝试使用 selenium 和 python 中的 ecommerce website 解析 html,以便从产品标题中获得关注 text:
“电源插座 220V 32A IP44”和“34.60”
问题:
我的变量 productCardTitle 和 productPriceValue 得到空值。
这是我的 html:
<ul class="products-cards products-cards--view-grid grid">
<li data-uid="210a10d0-4976-4c94-9ab4-0e7d38459c02" class="products-cards__item product-card">
<div class="product-card__top">...</div>
<div class="product-card__content">...</div>
<div class="product-card__title-wrapper">
<h3 class="product-card__title">
<a href="/catalog/Electric-outlet-220V-32A-IP44">
Electric outlet 220V 32A IP44
</a>
</h3>
</div>
...
<div class="product-card__bottom">
<p class="product-card__price product-price product-price--small">...</p>
<p class="product-card__price product-price">
<span class="visually-hidden">Price:</span>
<span class="product-price__value">34.60</span>
<span class="product-price__currency">...</span>
...
</li>
</ul>
这是我在 python 中的代码:
productCards = soup.find_all('li', class_="products-cards__item product-card")
for productCard in productCards:
productCardTitle = productCard.find_all('h3', class_="product-card__title")
for product in productCardTitle:
title = product.findChildren('a')[0].string.strip()
print(title)
productPriceValue = productCard.find_all('span', class_="product-price__value")
for product in productPriceValue:
price = product.string.strip()
print(price)
如果有人可以帮助我解决这个问题,我将不胜感激。
【问题讨论】:
-
空是什么意思?您的代码适用于您的示例。
-
我的意思是它实际上是空的。 Н我们的代码返回 [{'title': '', 'price': ''}]
-
正如@Corralien 提到的,发布的
html一切都很好,也许还有另一个问题(动态提供的内容),所以请提供您尝试抓取的url。 -
如何使用
requests或 Selenium 获取 html 页面? -
你说得对,很抱歉,发布的 html 一切正常。我会进一步查找错误。
标签: python html python-3.x parsing beautifulsoup