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