Input from file and output to file as json

This commit is contained in:
2024-01-30 19:01:14 +01:00
parent 3c0c748486
commit b7f71ab9f2
2 changed files with 24 additions and 8 deletions

View File

@@ -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)
save_to_json(product_info, output_file)

3
urls.txt Normal file
View File

@@ -0,0 +1,3 @@
https://www.lidl.cz/p/p100370600
https://www.lidl.cz/p/p100358513
https://www.lidl.cz/p/p100336045