對于產品串列中的一項,我有以下代碼:
<div class="nice_product_item">
<div class="npi_name">
<h2>
<a href="/solutii-mobile-telefoane-mobile/apple-telefon-mobil-apple-iphone-13-super-retina-xdr-oled-6.1-256gb-flash-camera-duala-12-12-mp-wi-fi-5g-ios-negru-3824456.html">
<span style="color:red">Stoc limitat!</span>
Telefon Mobil Apple iPhone 13, Super Retina XDR OLED 6.1", 256GB Flash, Camera Duala 12 12 MP, Wi-Fi, 5G, iOS (Negru)
</a>
</h2>
</div>
<div class="price_block_list">
<span class="old_price"> 999,00 Lei </span>
<span class="price_discount">-12%</span>
<span class="cheaper_by">mai ieftin cu 120,00 lei</span>
<span class="real_price">879,00 Lei</span>
<span class="evo-credit">evoCREDIT</span></div>
</div>
</div>
一些產品獲得了 price_discount 跨度,而另一些則沒有
<span class="price_discount">-12%</span>
我使用以下代碼來抓取產品名稱:
texts = []
for a in soup.select("div.npi_name a[href]"):
if a.span:
text = a.span.next_sibling
else:
text = a.string
texts.append(text.strip())
我不知道我需要什么條件才能獲得折扣產品的名稱。
注意:它必須適用于串列
uj5u.com熱心網友回復:
處理資料的一種方法可能是選擇所有有折扣的商品:
soup.select('div.nice_product_item:has(.price_discount):has(a[href])')
迭代ResultSet,選擇您需要的資訊并以結構化方式存盤它,如字典串列,以便稍后處理它,例如DataFrame并保存到 csv、json、...
例子
from bs4 import BeautifulSoup
import pandas as pd
html = '''
<div >
<div >
<h2>
<a href="/solutii-mobile-telefoane-mobile/apple-telefon-mobil-apple-iphone-13-super-retina-xdr-oled-6.1-256gb-flash-camera-duala-12-12-mp-wi-fi-5g-ios-negru-3824456.html">
<span style="color:red">Stoc limitat!</span>
Telefon Mobil Apple iPhone 13, Super Retina XDR OLED 6.1", 256GB Flash, Camera Duala 12 12 MP, Wi-Fi, 5G, iOS (Negru)
</a>
</h2>
</div>
<div >
<span > 999,00 Lei </span>
<span >-12%</span>
<span >mai ieftin cu 120,00 lei</span>
<span >879,00 Lei</span>
<span >evoCREDIT</span></div>
</div>
</div>
'''
soup = BeautifulSoup(html)
data = []
for e in soup.select('div.nice_product_item:has(.price_discount):has(a[href])'):
data.append({
'url' : e.a['href'],
'label' :s[-1] if (s := list(e.a.stripped_strings)) else None,
'price' : s.text if (s := e.select_one('span.real_price')) else None,
'discount' : s.text if (s := e.select_one('span.price_discount')) else None,
'other' : 'edit for elements you need'
})
pd.DataFrame(data)
輸出
| 網址 | 標簽 | 價錢 | 折扣 | 其他 |
|---|---|---|---|---|
| /solutii-mobile-telefoane-mobile/apple-telefon-mobil-apple-iphone-13-super-retina-xdr-oled-6.1-256gb-flash-camera-duala-12-12-mp-wi-fi-5g -ios-negru-3824456.html | Telefon Mobil Apple iPhone 13、Super Retina XDR OLED 6.1"、256GB 閃存、Duala 12 12 MP 攝像頭、Wi-Fi、5G、iOS (Negru) | 879,00 雷 | -12% | 編輯您需要的元素 |
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/448285.html
下一篇:我需要從類文本中提取id
