Read more
import xml.etree.ElementTree as ET
import requests
def create_product_html(xml_data):
"""
Parses XML data and generates HTML for a product.
Args:
xml_data: The XML string containing product information.
Returns:
An HTML string representing the product.
"""
try:
root = ET.fromstring(xml_data)
product_id = root.find('id').text if root.find('id') is not None else "N/A"
title = root.find('title').text if root.find('title') is not None else "N/A"
description = root.find('description').text if root.find('description') is not None else "N/A"
price = root.find('price').text if root.find('price') is not None else "N/A"
currency = root.find('price').get('currency') if root.find('price') is not None and root.find('price').get('currency') is not None else "USD" #Default to USD
image_url = root.find('imageUrl').text if root.find('imageUrl') is not None else "https://via.placeholder.com/200" #Default placeholder image
affiliate_url = root.find('affiliateUrl').text if root.find('affiliateUrl') is not None else "#" #Default link
store = root.find('store').text if root.find('store') is not None else "Unknown Store"
rating = root.find('rating').text if root.find('rating') is not None else "N/A"
num_reviews = root.find('numReviews').text if root.find('numReviews') is not None else "N/A"
# Load the HTML template (from a file or a string)
html_template = """
Product Details
"""
# Replace placeholders in the HTML template
html_output = html_template.format(
imageUrl=image_url,
title=title,
description=description,
currency=currency,
price=price,
rating=rating,
numReviews=num_reviews,
affiliateUrl=affiliate_url,
store=store
)
return html_output
except Exception as e:
print(f"Error processing XML: {e}")
return "
B07W4QYR66
Amazing Widget Pro
The best widget you'll ever need!
29.99
https://example.com/widget.jpg
https://amazon.com/dp/B07W4QYR66?tag=youraffiliatetag
Amazon
4.5
1234
"""
# Generate the HTML
product_html = create_product_html(xml_data)
# Print the HTML (in a real application, you would send this to the browser)
print(product_html)
{title}
{description}
Price: {currency} {price}
Rating: {rating} ({numReviews} reviews)
Store: {store}
Buy NowError displaying product.
" # Example usage: # 1. Get the XML data from Amazon or Flipkart API # 2. For this example, let's just read from a string: xml_data = """
0 Reviews