抓取一個在同一頁面上有多個產品的網站,有些我不想知道價格。所以我想先看產品類別,然后再列出價格。
網站代碼如下所示:
<section class="products_results">
<span something I don't want>...</span>
<section >
<span>Clothes</span>
<div something I don't want>...</div>
<section class="search_result_price">
<section>
<span something I don't want>...</span>
<span >149.99</span>
</section>
</section>
我已經知道如何使用自己的代碼進入類別部分,但我完全被困在另一部分。
for products in soup.find_all(class_='category'):
category = (products.text)
if category == 'Clothes':
price = (theoretical piece of code)
如何訪問此父標簽中的特定價格<section>標簽?
uj5u.com熱心網友回復:
從正則運算式使用
Import re
Pat = '''
price">(/d*./d*)</span>
'''
price = re.find_all(your text,pat)
uj5u.com熱心網友回復:
使用 CSS 選擇器,它按預期作業。
html='''
<section >
<span something I don't want>...</span>
<section >
<span>Clothes</span>
<div something I don't want>...</div>
<section >
<section>
<span something I don't want>...</span>
<span >149.99</span>
</section>
</section>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html,'lxml')
for p in soup.select('.category section.search_result_price span.price'):
print(p.text)
輸出:
149.99
uj5u.com熱心網友回復:
您已接近目標,但請注意,這products.text將為您提供整個部分的文本,更好地用于products.span.text僅獲取類別文本。
要獲取價格資訊,只需找到 span并檢查它是否可用以避免錯誤:
price = products.find(class_='price').text if products.find('span', class_='price') else None
例子
from bs4 import BeautifulSoup
html='''
<section >
<span something I don't want>...</span>
<section >
<span>Clothes</span>
<div something I don't want>...</div>
<section >
<section>
<span something I don't want>...</span>
<span >149.99</span>
</section>
</section>'''
soup = BeautifulSoup(html, 'html.parser')
for products in soup.find_all('section', class_='category'):
category = products.span.text
if category == 'Clothes':
price = products.find(class_='price').text if products.find('span', class_='price') else None
print(price)
輸出
149.99
作為替代方法,更精簡的方法是創建易于處理的結構化輸出并處理允許的類別串列:
from bs4 import BeautifulSoup
html='''
<section >
<span something I don't want>...</span>
<section >
<span>Clothes</span>
<div something I don't want>...</div>
<section >
<section>
<span something I don't want>...</span>
<span >149.99</span>
</section>
<span something I don't want>...</span>
<section >
<span>Shoes</span>
<div something I don't want>...</div>
<section >
<section>
<span something I don't want>...</span>
<span >90.99</span>
</section>
</section>'''
soup = BeautifulSoup(html, 'html.parser')
data = []
c_list = ['Clothes','Shoes']
for products in soup.select(f"section.category:-soup-contains({','.join(c_list)})"):
data.append({
'category' : products.span.text,
'price' : products.find(class_='price').text if products.find('span', class_='price') else None
})
data
輸出
[{'category': 'Clothes', 'price': '149.99'},
{'category': 'Shoes', 'price': '90.99'}]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/492294.html
下一篇:Golang表格抓取
