Spaces:
Paused
Paused
Update scraper.py
Browse files- scraper.py +38 -49
scraper.py
CHANGED
|
@@ -13,56 +13,45 @@ async def check_fragment(word: str) -> str:
|
|
| 13 |
url = f"https://fragment.com/username/{word}"
|
| 14 |
|
| 15 |
async with aiohttp.ClientSession() as session:
|
| 16 |
-
for _ in range(
|
| 17 |
-
|
| 18 |
headers = {"User-Agent": random.choice(USER_AGENTS)}
|
| 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 |
-
with open("other.txt", "a", encoding="utf-8") as f:
|
| 54 |
-
f.write(f"{word} - {clean_status}\n")
|
| 55 |
-
return clean_status
|
| 56 |
-
|
| 57 |
-
# 4. Final Fail-safes
|
| 58 |
-
if "tm-status-unavail" in html: return "UNAVAILABLE"
|
| 59 |
-
if "This username is unavailable" in html: return "UNAVAILABLE"
|
| 60 |
-
|
| 61 |
-
return "UNAVAILABLE"
|
| 62 |
-
|
| 63 |
-
except Exception:
|
| 64 |
-
pass
|
| 65 |
-
|
| 66 |
-
await asyncio.sleep(random.uniform(0.8, 2.0))
|
| 67 |
|
| 68 |
return "ERROR"
|
|
|
|
| 13 |
url = f"https://fragment.com/username/{word}"
|
| 14 |
|
| 15 |
async with aiohttp.ClientSession() as session:
|
| 16 |
+
for _ in range(4):
|
| 17 |
+
try:
|
| 18 |
headers = {"User-Agent": random.choice(USER_AGENTS)}
|
| 19 |
+
# allow_redirects=True so we can catch the canonical link
|
| 20 |
+
async with session.get(url, headers=headers, allow_redirects=True, timeout=12) as resp:
|
| 21 |
+
|
| 22 |
+
if resp.status in [429, 403]:
|
| 23 |
+
await asyncio.sleep(random.uniform(1.5, 3.5))
|
| 24 |
+
continue
|
| 25 |
|
| 26 |
+
html = await resp.text()
|
| 27 |
+
|
| 28 |
+
# 1. THE CANONICAL RULE (100% accurate Soft-404 detection)
|
| 29 |
+
# If Fragment drops us on the homepage, the canonical link changes.
|
| 30 |
+
canonical = re.search(r'<link rel="canonical" href="([^"]+)"', html)
|
| 31 |
+
if canonical and "/username/" not in canonical.group(1):
|
| 32 |
+
return "UNAVAILABLE"
|
| 33 |
+
|
| 34 |
+
# 2. EXACT STATUS EXTRACTION (From your uploaded HTML tags)
|
| 35 |
+
status_match = re.search(r'class="tm-section-header-status[^>]*>([^<]+)</span>', html, re.IGNORECASE)
|
| 36 |
+
|
| 37 |
+
if status_match:
|
| 38 |
+
s = status_match.group(1).strip().lower()
|
| 39 |
+
if "sold" in s: return "SOLD"
|
| 40 |
+
if "taken" in s: return "TAKEN"
|
| 41 |
+
if "auction" in s: return "ON_AUCTION"
|
| 42 |
+
if "available" in s: return "AVAILABLE"
|
| 43 |
+
|
| 44 |
+
# 3. LOG UNKNOWN STATUSES
|
| 45 |
+
clean_status = s.upper()
|
| 46 |
+
with open("other.txt", "a", encoding="utf-8") as f:
|
| 47 |
+
f.write(f"{word} - {clean_status}\n")
|
| 48 |
+
return clean_status
|
| 49 |
+
|
| 50 |
+
# 4. Final Fallback
|
| 51 |
+
if "tm-status-unavail" in html: return "UNAVAILABLE"
|
| 52 |
+
|
| 53 |
+
except Exception:
|
| 54 |
+
await asyncio.sleep(1)
|
| 55 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
return "ERROR"
|