File size: 8,196 Bytes
895145e 7e7d367 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
---
license: mit
---
## FalconNet/FunPay-Minecraft-Lots-Mini-6k
- Prices are in Rubles
### Script used to create this:
```python
from __future__ import annotations
import argparse
import asyncio
import csv
import json
import re
from dataclasses import dataclass, asdict
from pathlib import Path
from typing import List, Optional
from bs4 import BeautifulSoup
from playwright.async_api import async_playwright, TimeoutError as PlaywrightTimeout
@dataclass
class Lot:
"""Represents a single offer on FunPay."""
lot_id: int
server: str
title: str
category: str
seller: str
reviews: int
price: float
currency: str
link: str
class FunpayParser:
"""High-level API for scraping the public FunPay catalog."""
BASE_URL = "https://funpay.com"
def __init__(
self,
language: str = "ru",
currency: str = "RUB",
headless: bool = False,
timeout: int = 10_000,
) -> None:
self.language = language
self.currency = currency
self.headless = headless
self.timeout = timeout
# ─────────────────────────── internal helpers ──────────────────────────── #
async def _fetch_html(self, url: str) -> str:
"""Render *url* with Playwright and return the final HTML source."""
async with async_playwright() as p:
browser = await p.chromium.launch(headless=self.headless)
context = await browser.new_context(
locale=self.language,
extra_http_headers={
"Accept-Language": self.language,
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/124.0.0.0 Safari/537.36"
),
},
)
# Pre-set currency so the prices are parsed consistently
await context.add_cookies(
[
{
"name": "cy",
"value": self.currency,
"domain": "funpay.com",
"path": "/",
}
]
)
page = await context.new_page()
try:
# Navigate and wait for the full page load, including JS content
await page.goto(url, wait_until="load", timeout=self.timeout)
# Явное ожидание появления важного элемента (например, списка лотов)
await page.wait_for_selector(".tc-item", timeout=10_000) # Ждем появления первого лота
except PlaywrightTimeout:
await browser.close()
raise RuntimeError(f"Page load timeout: {url}")
# Accept cookies banner if present
try:
await page.locator("button:has-text('Agree')").click(timeout=2_000)
except PlaywrightTimeout:
pass # banner not shown
html = await page.content()
await browser.close()
return html
def _parse_html(self, html: str) -> List[Lot]:
"""Extract structured data from the rendered HTML using BeautifulSoup."""
soup = BeautifulSoup(html, "lxml")
lots: List[Lot] = []
for anchor in soup.select("a.tc-item"):
link = self.BASE_URL + anchor.get("href", "")
# Server / sub-category (can be empty)
server_el = anchor.select_one(".tc-server")
server = server_el.get_text(strip=True) if server_el else ""
# Title / description
title_el = anchor.select_one(".tc-desc") or anchor.select_one(".tc-title")
title = title_el.get_text(strip=True) if title_el else ""
# Small badge with an additional category mark
badge_el = anchor.select_one(".badge")
category = badge_el.get_text(strip=True) if badge_el else ""
# Seller block
seller_el = anchor.select_one(".media-user-name")
seller = seller_el.get_text(strip=True) if seller_el else ""
# Reviews are rendered as “(1234)”
rev_el = anchor.select_one(".rating-count")
reviews = int(rev_el.get_text(strip=True).strip("()")) if rev_el else 0
# Price block
price_el = anchor.select_one(".tc-price")
price_val, price_curr = 0.0, self.currency
if price_el:
match = re.match(r"([\d\.,]+)\s*([^\d\s]+)", price_el.get_text(strip=True))
if match:
raw, price_curr = match.groups()
price_val = float(raw.replace(",", "."))
# Lot ID is available only in the lot link
match_id = re.search(r"id=(\d+)", link)
lot_id = int(match_id.group(1)) if match_id else 0
lots.append(
Lot(
lot_id=lot_id,
server=server,
title=title,
category=category,
seller=seller,
reviews=reviews,
price=price_val,
currency=price_curr,
link=link,
)
)
return lots
async def _collect_lots(self, section_id: int, pages: int) -> List[Lot]:
"""Coroutine that loads *pages* pages for *section_id* sequentially."""
result: List[Lot] = []
for idx in range(1, pages + 1):
url = f"{self.BASE_URL}/{self.language}/lots/{section_id}/?page={idx}"
html = await self._fetch_html(url)
page_lots = self._parse_html(html)
if not page_lots:
break # reached last page
result.extend(page_lots)
return result
# ───────────────────────────── public API ──────────────────────────────── #
def parse(self, section_id: int, pages: int = 1) -> List[Lot]:
"""Synchronously scrape *pages* from a lots section."""
return asyncio.run(self._collect_lots(section_id, pages))
# I/O helpers remain unchanged
def save_json(lots: List[Lot], path: Path) -> None:
path.write_text(json.dumps([asdict(x) for x in lots], ensure_ascii=False, indent=2), "utf-8")
def save_csv(lots: List[Lot], path: Path) -> None:
if not lots:
return
with path.open("w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=asdict(lots[0]).keys())
writer.writeheader()
for row in lots:
writer.writerow(asdict(row))
# CLI entry unchanged
if __name__ == "__main__":
def _cli() -> None:
parser = argparse.ArgumentParser(description="Scrape FunPay lots into JSON/CSV files")
parser.add_argument("section", type=int, help="Section numeric ID (e.g. 223 for Minecraft Services)")
parser.add_argument("--pages", type=int, default=1, help="Number of paginated screens to scan")
parser.add_argument("--out", type=Path, help="Output file (.json or .csv)")
parser.add_argument("--headed", action="store_true", help="Run browser in headed mode (for debugging)")
args = parser.parse_args()
funpay = FunpayParser(headless=not args.headed)
lots = funpay.parse(args.section, pages=args.pages)
print(f"Collected {len(lots)} lots from section #{args.section}")
if args.out:
if args.out.suffix == ".json":
save_json(lots, args.out)
elif args.out.suffix == ".csv":
save_csv(lots, args.out)
else:
raise SystemExit("Supported output formats: .json / .csv")
print(f"Saved to {args.out.resolve()}")
else:
from pprint import pprint
pprint(lots[:10])
``` |