Skip to content

Commit c1b3a26

Browse files
committed
fix: return dictionary and try exception for each locators products
1 parent a287a7a commit c1b3a26

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

search_product.py

+22-10
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,31 @@ async def search_product(product_name):
3030
product_url = page.url
3131

3232
product_name = await page.locator('h1.brTtKt').inner_text()
33-
old_price = float((await page.locator('.oldPrice').inner_text()).replace('R$', '').replace(',', '.').strip())
34-
new_price = float((await page.locator('.finalPrice').inner_text()).replace('R$', '').replace(',', '.').strip())
35-
installment_price = float((await page.locator('.regularPrice').inner_text()).replace('R$', '').replace(',', '.').strip())
3633

37-
await browser.close()
34+
try:
35+
old_price = float((await page.locator('.oldPrice').inner_text()).replace('R$', '').replace(',', '.').strip())
36+
except Exception:
37+
old_price = 'N/A'
38+
39+
try:
40+
new_price = float((await page.locator('.finalPrice').inner_text()).replace('R$', '').replace(',', '.').strip())
41+
except Exception:
42+
new_price = 'N/A'
3843

39-
message = f'Produto: {product_name}\n'
40-
message += f'Preço Antigo: R$ {old_price:.2f}\n' if old_price else 'Preço Antigo: Não disponível\n'
41-
message += f'Preço Novo: R$ {new_price:.2f}\n' if new_price else 'Preço Novo: Não disponível\n'
42-
message += f'Preço em Parcelas: R$ {installment_price:.2f}\n' if installment_price else 'Preço em Parcelas: Não disponível\n'
43-
message += f'Link do Produto: {product_url}'
44+
try:
45+
installment_price = float((await page.locator('.regularPrice').inner_text()).replace('R$', '').replace(',', '.').strip())
46+
except Exception:
47+
installment_price = 'N/A'
48+
49+
await browser.close()
4450

45-
return message
51+
return {
52+
'nome': product_name,
53+
'preco_antigo': old_price,
54+
'preco_novo': new_price,
55+
'preco_parcelas': installment_price,
56+
'link': product_url
57+
}
4658

4759
except Exception as e:
4860
return f'Erro durante a automação: {str(e)}'

0 commit comments

Comments
 (0)