diff --git a/scrape.py b/scrape.py index 9dc6926..e02ed6f 100644 --- a/scrape.py +++ b/scrape.py @@ -1,3 +1,4 @@ +import json import requests from bs4 import BeautifulSoup from tabulate import tabulate @@ -16,7 +17,13 @@ def fetch_product_info(urls): discount_element = soup.find('div', class_='m-price__label') discount = discount_element.text.strip() if discount_element else "-" - product_info.append([product_name, current_price, discount, original_price, url]) + product_info.append({ + "name": product_name, + "price": current_price, + "discount": discount, + "originalPrice": original_price, + "url": url + }) else: print(f"Failed to fetch URL: {url}") @@ -24,14 +31,20 @@ def fetch_product_info(urls): def print_product_table(product_info): headers = ["Product Name", "Price", "Discount", "original price", "URL"] - print(tabulate(product_info, headers=headers, tablefmt="grid")) + table_data = [[info["name"], info["price"], info["discount"], info["originalPrice"], info["url"]] for info in product_info] + print(tabulate(table_data, headers=headers, tablefmt="grid")) + +def save_to_json(product_info, output_file): + with open(output_file, 'w') as f: + json.dump(product_info, f, indent=4) if __name__ == "__main__": - urls = [ - "https://www.lidl.cz/p/p100370600", - "https://www.lidl.cz/p/p100358513", - "https://www.lidl.cz/p/p100336045", - ] + input_file = "urls.txt" + output_file = "product_info.json" + + with open(input_file, 'r') as f: + urls = [line.strip() for line in f.readlines()] product_info = fetch_product_info(urls) - print_product_table(product_info) \ No newline at end of file + print_product_table(product_info) + save_to_json(product_info, output_file) \ No newline at end of file diff --git a/urls.txt b/urls.txt new file mode 100644 index 0000000..8b2be0a --- /dev/null +++ b/urls.txt @@ -0,0 +1,3 @@ +https://www.lidl.cz/p/p100370600 +https://www.lidl.cz/p/p100358513 +https://www.lidl.cz/p/p100336045 \ No newline at end of file