我开发JavaScript已经有一段时间了,但是Python对我来说还是有点新鲜。我试着用Python从一个简单的网页上获取内容(基本上是一个包含不同部分的产品列表)。内容是动态生成的,因此使用selenium模块来实现这一点。在
内容结构如下所示,包含几个产品部分:
<div class="product-section">
<div class="section-title">
Product section name
</div>
<ul class="products">
<li class="product">
<div class="name">Wooden Table</div>
<div class="price">99 USD</div>
<div class="color">White</div>
</li>
</ul>
</div>
用于抓取产品的Python代码:
^{pr2}$现在我得到了所有产品的属性(见下文),但我无法将它们与不同的部分分开。在
当前结果
木制桌子,99美元,白色
草坪椅,39美元,黑色
帐篷-4人,299美元,迷彩
等等
预期结果:
室外家具
木制桌子,99美元,白色
草坪椅,39美元,黑色
野营装备
帐篷-4人,299美元,迷彩
保温瓶,19美元,金属
最终目标是将内容输出到一个excel产品列表中,因此我需要将这些部分分开(与它们匹配的部分标题)。有没有办法让它们分开,即使它们有相同的类名?在
你就快到了——按部分对产品进行分组,然后从一个小节开始,找到其中的所有元素。至少你的示例html暗示了它的结构允许它。在
基于您的代码,这里有一个带有解释性注释的解决方案。在
相关问题 更多 >
编程相关推荐