import requests from bs4 import BeautifulSoup def fetch_products_from_html(html): soup = BeautifulSoup(html, features="html.parser") refurb_div = soup.find('div', {'class': 'refurbished-category-grid-no-js'}) return refurb_div.findAll('li') def pretty_print_prduct_details(products): for product in products: product_name = product.find('h3').text.strip().replace('\n', '') current_price = product.find('div', {'class': 'as-producttile-currentprice'}).text.strip().replace('\n', '') old_price = product.find('span', {'class': 'as-price-previousprice'}).text.strip().replace('\n', '') saving = product.find('span', {'class': 'as-producttile-savingsprice'}).text.strip().replace('\n', '') print(f' Name: {product_name}, price: {current_price}, {old_price}, {saving}') def fetch_and_print_product_details_from_url(url): html = requests.get(url).text products = fetch_products_from_html(html) print(f' Number of products found: {len(products)}') pretty_print_prduct_details(products) if __name__ == '__main__': ipad_url = 'https://www.apple.com/ca/shop/refurbished/ipad/' print('Ipads:') fetch_and_print_product_details_from_url(ipad_url) print("~" * 20) accessories_url = 'https://www.apple.com/ca/shop/refurbished/accessories' print('Accessories:') fetch_and_print_product_details_from_url(accessories_url)