Spaces:
Paused
Paused
Create parser.py
Browse files
parser.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from bs4 import BeautifulSoup
|
| 2 |
+
|
| 3 |
+
def parse_html(html: str, final_url: str, word: str) -> str:
|
| 4 |
+
soup = BeautifulSoup(html, 'lxml')
|
| 5 |
+
full_text = soup.get_text(separator=' ', strip=True).lower()
|
| 6 |
+
is_direct = '/username/' in final_url and '?query=' not in final_url
|
| 7 |
+
|
| 8 |
+
if is_direct:
|
| 9 |
+
status_tag = soup.find(class_='tm-section-header-status')
|
| 10 |
+
if status_tag:
|
| 11 |
+
t = status_tag.get_text(strip=True).lower()
|
| 12 |
+
if 'taken' in t or 'sold' in t: return 'TAKEN'
|
| 13 |
+
if 'available' in t: return 'AVAILABLE'
|
| 14 |
+
if 'on auction' in t: return 'ON_AUCTION'
|
| 15 |
+
if 'for sale' in t: return 'FOR_SALE'
|
| 16 |
+
|
| 17 |
+
if 'is already taken' in full_text:
|
| 18 |
+
return 'TAKEN'
|
| 19 |
+
|
| 20 |
+
rows = soup.find_all('tr')
|
| 21 |
+
for row in rows:
|
| 22 |
+
text = row.get_text(separator=' ', strip=True).lower()
|
| 23 |
+
if f"@{word}" in text.split():
|
| 24 |
+
if 'unavailable' in text and 'unknown' in text: return 'UNAVAILABLE'
|
| 25 |
+
if 'on auction' in text: return 'ON_AUCTION'
|
| 26 |
+
if 'for sale' in text and 'not for sale' not in text: return 'FOR_SALE'
|
| 27 |
+
if 'available' in text: return 'AVAILABLE'
|
| 28 |
+
if 'sold' in text: return 'TAKEN'
|
| 29 |
+
if 'unavailable' in text: return 'UNAVAILABLE'
|
| 30 |
+
|
| 31 |
+
return 'UNCERTAIN'
|