#Code Generated by Parser:396e8ed9 Rule: 2025-01-05 11:10:46
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
from lxml.cssselect import CSSSelector
from decimal import Decimal
import re
def extract_SellPrice(lxml_tree):
selector = CSSSelector("#corePrice_desktop .a-price .a-offscreen")
elements = selector(lxml_tree)
if elements:
price_text = elements[0].text_content()
price_text = re.sub(r'\.\.', '.', price_text)
return Decimal(re.sub(r'[^\d.]', '', price_text))
return Decimal('0')
def extract_SellPrice2(lxml_tree):
selector = CSSSelector("#corePrice_desktop .a-price .a-offscreen")
elements = selector(lxml_tree)
prices = []
for element in elements:
price_text = element.text_content()
price_text = re.sub(r'\.\.', '.', price_text)
prices.append(Decimal(re.sub(r'[^\d.]', '', price_text)))
return max(prices) if prices else Decimal('0')
def extract_Location(lxml_tree):
from lxml.cssselect import CSSSelector
import re
selector = CSSSelector("span#glow-ingress-line2")
result = selector(lxml_tree)
return re.sub(r'\s+', ' ', result[0].text_content().strip()) if result else None
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())) if result else None
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(re.sub(r'[^\d.]', '', result[0].text_content())) if result else None
if __name__ == '__main__':
import lxml.html
url='<URL>'
html=get_html(url)
tree = lxml.html.fromstring(html)
result={}
result['SellPrice']=extract_SellPrice(tree)
result['SellPrice2']=extract_SellPrice2(tree)
result['Location']=extract_Location(tree)
result['ProductName']=extract_ProductName(tree)
result['TotalReview']=extract_TotalReview(tree)
result['ProductImage']=extract_ProductImage(tree)
result['AverageReview']=extract_AverageReview(tree)
print(result)