#Code Generated by Parser:f4f01f4b Rule: 2025-01-11 03:13:12
def get_html(url):
import time
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
page = browser.new_page()
page.goto(url)
time.sleep(10)
page_source = page.content()
browser.close()
with open("debug.html", "w", encoding="utf-8") as file:
file.write(page_source)
return page_source
def extract_ListPrice(lxml_tree):
from lxml.cssselect import CSSSelector
from decimal import Decimal
import re
selector = CSSSelector("span.strike-through.list .value")
result = selector(lxml_tree)
if result:
price_text = result[0].text_content()
price_cleaned = re.sub(r"[^\d.]", "", price_text)
return Decimal(price_cleaned)
return Decimal("0.00")
def extract_SellPrice(lxml_tree):
from lxml.cssselect import CSSSelector
from decimal import Decimal
import re
selector = CSSSelector("span.sales.font-body-large")
result = selector(lxml_tree)
if result:
price_text = result[0].text_content()
price_cleaned = re.sub(r"[^\d.]", "", price_text).replace("..", ".")
return Decimal(price_cleaned)
return Decimal("0.00")
def extract_Discount(lxml_tree):
from lxml.cssselect import CSSSelector
import re
selector = CSSSelector('.percentOff')
result = selector(lxml_tree)
return re.search(r'(\d+)%', result[0].text).group(1) if result else None
def extract_ProductName(lxml_tree):
from lxml.cssselect import CSSSelector
import re
selector = CSSSelector('.product-name.hidden-sm-down')
result = selector(lxml_tree)
return re.sub(r'\s+', ' ', result[0].text.strip()) if result else None
def extract_TotalReview(lxml_tree):
from lxml.cssselect import CSSSelector
import re
selector = CSSSelector('.bv_numReviews_text')
result = selector(lxml_tree)
return int(re.search(r'\((\d+)\)', result[0].text).group(1)) if result else None
def extract_Availability(lxml_tree):
from lxml.cssselect import CSSSelector
selector = CSSSelector('.bossPromotions p')
result = selector(lxml_tree)
return bool(result)
def extract_ProductImage(lxml_tree):
from lxml.cssselect import CSSSelector
selector = CSSSelector('.tile-image')
result = selector(lxml_tree)
return result[0].get('src') if result else None
def extract_AverageReview(lxml_tree):
from lxml.cssselect import CSSSelector
import re
selector = CSSSelector('.bv_avgRating_component_container')
result = selector(lxml_tree)
return float(result[0].text.strip()) if result else None
if __name__ == '__main__':
import lxml.html
url='<URL>'
html=get_html(url)
tree = lxml.html.fromstring(html)
result={}
result['ListPrice']=extract_ListPrice(tree)
result['SellPrice']=extract_SellPrice(tree)
result['Discount']=extract_Discount(tree)
result['ProductName']=extract_ProductName(tree)
result['TotalReview']=extract_TotalReview(tree)
result['Availability']=extract_Availability(tree)
result['ProductImage']=extract_ProductImage(tree)
result['AverageReview']=extract_AverageReview(tree)
print(result)