Calculate status text manually if it's not returned by PlayWright API

This commit is contained in:
Karim shoair
2024-11-20 13:27:18 +02:00
parent 4c74d9bc97
commit 19ad82caef
4 changed files with 94 additions and 3 deletions
+7 -1
View File
@@ -4,6 +4,7 @@ from scrapling.core._types import Union, Callable, Optional, Dict, List, Literal
from scrapling.engines.toolbelt import (
Response,
do_nothing,
StatusText,
get_os_name,
intercept_route,
check_type_validity,
@@ -111,12 +112,17 @@ class CamoufoxEngine:
if 'charset=' in content_type.lower():
encoding = content_type.lower().split('charset=')[-1].split(';')[0].strip()
status_text = res.status_text
# PlayWright API sometimes give empty status text for some reason!
if not status_text:
status_text = StatusText.get(res.status)
response = Response(
url=res.url,
text=page.content(),
body=page.content().encode('utf-8'),
status=res.status,
reason=res.status_text,
reason=status_text,
encoding=encoding,
cookies={cookie['name']: cookie['value'] for cookie in page.context.cookies()},
headers=res.all_headers(),