#Code Generated by Parser:9e0b9a52 Rule: 2025-01-07 14:51:01
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_SellPrice(lxml_tree):
from lxml.cssselect import CSSSelector
import re
from decimal import Decimal
selector = CSSSelector("div#corePrice_desktop span.a-offscreen")
result = selector(lxml_tree)
if result:
price_text = result[0].text_content()
clean_price = re.sub(r'[^\d\.]', '', price_text).replace('..', '.')
return Decimal(clean_price)
def extract_SellPrice2(lxml_tree):
from lxml.cssselect import CSSSelector
import re
from decimal import Decimal
try:
selector = CSSSelector("div#corePrice_desktop span.a-price-range span.a-offscreen")
result = selector(lxml_tree)
if result:
prices = [Decimal(re.sub(r'[^\d\.]', '', el.text_content()).replace('..', '.')) for el in result]
return max(prices)
except Exception:
return extract_SellPrice(lxml_tree)
def extract_ProductName(lxml_tree):
from lxml.cssselect import CSSSelector
import re
selector = CSSSelector("span#productTitle")
result = selector(lxml_tree)
return re.sub(r'\s+', ' ', result[0].text_content().strip()) if result else None
def extract_TotalReview(lxml_tree):
from lxml.cssselect import CSSSelector
import re
selector = CSSSelector("span#acrCustomerReviewText")
result = selector(lxml_tree)
return int(re.sub(r'[^\d]', '', result[0].text_content().strip())) if result else None
def extract_Availability(lxml_tree):
from lxml.cssselect import CSSSelector
selector = CSSSelector("span#creturns-return-policy-message")
result = selector(lxml_tree)
return bool(result)
def extract_ProductImage(lxml_tree):
from lxml.cssselect import CSSSelector
selector = CSSSelector("img#landingImage")
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("span#acrPopover span.a-size-base.a-color-base")
result = selector(lxml_tree)
return float(result[0].text_content().strip()) if result else None
if __name__ == '__main__':
import lxml.html
url='https://www.amazon.com/HUNGSON-Pockets-Stretch-Skinny-Fashion/dp/B0BQHT52XB'
html=get_html(url)
tree = lxml.html.fromstring(html)
result={}
result['SellPrice']=extract_SellPrice(tree)
result['SellPrice2']=extract_SellPrice2(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)