56 lines
1.6 KiB
Python
56 lines
1.6 KiB
Python
from os import PathLike
|
|
from pathlib import Path
|
|
import re
|
|
from playwright.sync_api import sync_playwright, Browser
|
|
from contextlib import contextmanager
|
|
|
|
|
|
@contextmanager
|
|
def run_browser(data_dir: PathLike, extension_path: PathLike) -> Browser:
|
|
with sync_playwright() as pw:
|
|
with pw.chromium.launch_persistent_context(
|
|
user_data_dir=data_dir,
|
|
headless=False,
|
|
args=[
|
|
f"--disable-extensions-except={extension_path}",
|
|
f"--load-extension={extension_path}",
|
|
],
|
|
) as browser:
|
|
yield browser
|
|
|
|
|
|
def find_sales_performance(browser: Browser, url: str) -> dict:
|
|
with browser.new_page() as page:
|
|
page.goto(url)
|
|
text = page.wait_for_selector("#h10-product-score").inner_text()
|
|
|
|
amazon_id = re.search(r'/dp/([^?]+)', url).group(1)
|
|
score = float(re.search(r"(\d+\.?\d*)", text).group(1))
|
|
try:
|
|
sales = int(re.search(r"Sales\s*(\d+[,.]?\d*)", text, re.MULTILINE).group(1).replace(',', ""))
|
|
except:
|
|
sales = -1
|
|
|
|
return {
|
|
'amazon_id': amazon_id,
|
|
'sales': sales,
|
|
'score': score,
|
|
}
|
|
|
|
|
|
extension_path = Path(r"~/Desktop/amazon").expanduser().resolve()
|
|
data_dir = Path(r'~/Desktop/amazondemo').expanduser()
|
|
|
|
def main():
|
|
urls = [
|
|
"https://www.amazon.co.uk/dp/B07B9G7V3P?psc=1",
|
|
]
|
|
with run_browser(data_dir=data_dir, extension_path=extension_path) as browser:
|
|
for url in urls:
|
|
perf = find_sales_performance(browser=browser, url=url)
|
|
print(perf)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|