fix: _parse
This commit is contained in:
+14
-5
@@ -66,17 +66,26 @@ class IPInfo:
|
||||
def _parse(self, html: str) -> Optional[IPInfoModel]:
|
||||
"""Парсит HTML и возвращает модель"""
|
||||
soup = BeautifulSoup(html, 'html.parser')
|
||||
script = soup.find('script', type='application/ld+json')
|
||||
scripts = soup.find_all('script', type='application/ld+json')
|
||||
|
||||
if not script or not script.string:
|
||||
if not scripts:
|
||||
return None
|
||||
|
||||
# Ищем первый скрипт с типом DataFeed
|
||||
data = None
|
||||
for script in scripts:
|
||||
if not script.string:
|
||||
continue
|
||||
|
||||
try:
|
||||
data: Dict = json.loads(script.string.strip())
|
||||
parsed = json.loads(script.string.strip())
|
||||
if parsed.get('@type') == 'DataFeed':
|
||||
data = parsed
|
||||
break
|
||||
except json.JSONDecodeError:
|
||||
return None
|
||||
continue
|
||||
|
||||
if data.get('@type') != 'DataFeed':
|
||||
if data is None:
|
||||
return None
|
||||
|
||||
# Базовые поля
|
||||
|
||||
Reference in New Issue
Block a user