-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathapp_2.py
28 lines (24 loc) · 1.21 KB
/
app_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import requests
from bs4 import BeautifulSoup # pip install beautifulsoup4
def fetch_page():
url = 'https://www.mercadolivre.com.br/apple-iphone-16-pro-1-tb-titnio-preto-distribuidor-autorizado/p/MLB1040287851#polycard_client=search-nordic&wid=MLB5054621110&sid=search&searchVariation=MLB1040287851&position=6&search_layout=stack&type=product&tracking_id=92c2ddf6-f70e-475b-b41e-fe2742459774'
response = requests.get(url)
return response.text
def parse_page(html):
soup = BeautifulSoup(html, 'html.parser')
product_name = soup.find('h1', class_='ui-pdp-title').get_text(strip=True)
prices = soup.find_all('span', class_='andes-money-amount__fraction')
old_price = int(prices[0].get_text(strip=True).replace('.', ''))
new_price = int(prices[1].get_text(strip=True).replace('.', ''))
installment_price = int(prices[2].get_text(strip=True).replace('.', ''))
return {
'product_name': product_name,
'old_price': old_price,
'new_price': new_price,
'installment_price': installment_price
}
# Teste das funções
if __name__ == '__main__':
page_content = fetch_page()
product_info = parse_page(page_content)
print(product_info)