@@ -30,19 +30,31 @@ async def search_product(product_name):
30
30
product_url = page .url
31
31
32
32
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 ())
36
33
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'
38
43
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 ()
44
50
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
+ }
46
58
47
59
except Exception as e :
48
60
return f'Erro durante a automação: { str (e )} '
0 commit comments