|
| 1 | +import re |
| 2 | +from urllib.parse import quote_plus |
| 3 | + |
| 4 | +from retailpricescraper.data import GraphicNovel |
| 5 | +from retailpricescraper.scraper import Scraper |
| 6 | + |
| 7 | + |
| 8 | +class InStockTrades(Scraper): |
| 9 | + def __init__(self, browser): |
| 10 | + self.browser = browser |
| 11 | + |
| 12 | + async def scrape(self, isbn: str, title: str) -> GraphicNovel: |
| 13 | + context = await self.browser.new_context( |
| 14 | + user_agent="Mozilla/5.0 ... Chrome/137", |
| 15 | + viewport={"width": 1280, "height": 800}, |
| 16 | + locale="en-US", |
| 17 | + ) |
| 18 | + |
| 19 | + page = await context.new_page() |
| 20 | + page.set_default_timeout(8000) |
| 21 | + |
| 22 | + await page.goto( |
| 23 | + f"https://www.instocktrades.com/search?term={quote_plus(title)}", |
| 24 | + timeout=180000, |
| 25 | + ) |
| 26 | + |
| 27 | + price = None |
| 28 | + url = None |
| 29 | + |
| 30 | + try: |
| 31 | + title_text = await page.locator(".title").first.inner_text() |
| 32 | + |
| 33 | + title_text = title_text.lower() |
| 34 | + title_text = re.sub(r"\b(tp|hc)\b", "", title_text) |
| 35 | + title_text = re.sub(r"[^a-z0-9\s]", " ", title_text) |
| 36 | + title_text = re.sub(r"\s+", " ", title_text).strip() |
| 37 | + |
| 38 | + title = title.lower() |
| 39 | + title = re.sub(r"[^a-z0-9\s]", " ", title) |
| 40 | + title = re.sub(r"\s+", " ", title).strip() |
| 41 | + |
| 42 | + if title in title_text: |
| 43 | + title = await page.locator(".title").first.inner_text() |
| 44 | + |
| 45 | + price_text = await page.locator(".price").first.inner_text() |
| 46 | + price = float(price_text.replace("$", "").strip()) |
| 47 | + |
| 48 | + href = ( |
| 49 | + (await page.locator(".title a").first.get_attribute("href")) |
| 50 | + .strip() |
| 51 | + .split("?", 1)[0] |
| 52 | + ) |
| 53 | + url = f"https://www.instocktrades.com{href}" |
| 54 | + else: |
| 55 | + title = "" |
| 56 | + except Exception as e: |
| 57 | + print(f"Unable to scrape {isbn} from In Stock Trades: {e}") |
| 58 | + |
| 59 | + await page.close() |
| 60 | + |
| 61 | + return GraphicNovel( |
| 62 | + isbn=isbn, |
| 63 | + retailer="In Stock Trades", |
| 64 | + title=title, |
| 65 | + price=price, |
| 66 | + url=url, |
| 67 | + ) |
0 commit comments