22
33from __future__ import annotations
44
5- import asyncio
65import json
6+ from types import MethodType
7+ from ..tools import AwaitableDict
78
89from playwright .async_api import Response as PWResponse
910from dataclasses import dataclass
@@ -120,12 +121,12 @@ async def info(
120121 category : str | None = None ,
121122 product_id : int | None = None ,
122123 slug : str | None = None ,
123- ) -> dict :
124+ ) -> PWResponse :
124125 """
125126 Информация СПАРСИВАЕТСЯ (в отличии от других методов).
126127 Инфо о товаре со страницы типа
127128 https://fix-price.com/catalog/produkty-i-napitki/p-1902248-shokoladnye-konfety-inis-nickers-135-g
128-
129+
129130 Либо предоставляете url напрямую, например `products[0]["url"]`
130131
131132 Данные карточки лежат в obj["data"][0]["categoryData"]["product"]
@@ -141,33 +142,42 @@ async def info(
141142 real_url += url
142143
143144 page = await self ._parent .ctx .new_page ()
144- await page .goto (real_url , wait_until = "domcontentloaded" )
145+ try :
146+ resp = await page .goto (real_url , wait_until = "domcontentloaded" )
147+ if resp is None :
148+ raise RuntimeError ("page.goto() returned None" )
145149
146- raw_json = await page .evaluate ("""
147- () => {
148- const marker = "window.__NUXT__=";
150+ raw_json = await page .evaluate ("""
151+ () => {
152+ const marker = "window.__NUXT__=";
149153
150- for (const s of document.scripts) {
151- const txt = s.textContent || "";
152- const idx = txt.indexOf(marker);
154+ for (const s of document.scripts) {
155+ const txt = s.textContent || "";
156+ const idx = txt.indexOf(marker);
153157
154- if (idx !== -1) {
155- let expr = txt.slice(idx + marker.length).trim();
158+ if (idx !== -1) {
159+ let expr = txt.slice(idx + marker.length).trim();
156160
157- if (expr.endsWith(";")) {
158- expr = expr.slice(0, -1);
159- }
161+ if (expr.endsWith(";")) {
162+ expr = expr.slice(0, -1);
163+ }
160164
161- const obj = Function('"use strict"; return (' + expr + ')')();
162- return JSON.stringify(obj);
165+ const obj = Function('"use strict"; return (' + expr + ')')();
166+ return JSON.stringify(obj);
167+ }
163168 }
169+
170+ return null;
164171 }
172+ """ )
165173
166- return null;
167- }
168- """ )
169- nuxt_data = json .loads (raw_json )["data" ][0 ]["categoryData" ]["product" ] if raw_json else None
174+ nuxt_data = json .loads (raw_json )["data" ][0 ]["categoryData" ]["product" ] if raw_json else None
175+
176+ def _json (self ):
177+ return AwaitableDict (nuxt_data )
178+
179+ resp .json = MethodType (_json , resp )
170180
171- await page . close ()
172-
173- return nuxt_data
181+ return resp
182+ finally :
183+ await page . close ()
0 commit comments