Last active
September 13, 2020 04:24
-
-
Save heaven00/dacf529894eb38fc513a4f494a6f1833 to your computer and use it in GitHub Desktop.
something
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment