|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import asyncio |
| 6 | +import json |
| 7 | + |
| 8 | +from playwright.async_api import Response as PWResponse |
5 | 9 | from dataclasses import dataclass |
6 | | -from typing import Optional, TYPE_CHECKING |
| 10 | +from typing import Optional, TYPE_CHECKING, overload |
7 | 11 | from .. import abstraction |
8 | 12 | from human_requests import autotest |
9 | 13 | from human_requests.abstraction import FetchResponse, HttpMethod |
@@ -101,3 +105,69 @@ async def balance( |
101 | 105 | url += "&inStock=true" |
102 | 106 |
|
103 | 107 | return await self._parent._request(HttpMethod.GET, url) |
| 108 | + |
| 109 | + @overload |
| 110 | + async def info(self, *, url: str): ... |
| 111 | + |
| 112 | + @overload |
| 113 | + async def info(self, *, category: str, product_id: int, slug: str): ... |
| 114 | + |
| 115 | + @autotest |
| 116 | + async def info( |
| 117 | + self, |
| 118 | + *, |
| 119 | + url: str | None = None, |
| 120 | + category: str | None = None, |
| 121 | + product_id: int | None = None, |
| 122 | + slug: str | None = None, |
| 123 | + ) -> dict: |
| 124 | + """ |
| 125 | + Информация СПАРСИВАЕТСЯ (в отличии от других методов). |
| 126 | + Инфо о товаре со страницы типа |
| 127 | + https://fix-price.com/catalog/produkty-i-napitki/p-1902248-shokoladnye-konfety-inis-nickers-135-g |
| 128 | + |
| 129 | + Либо предоставляете url напрямую, например `products[0]["url"]` |
| 130 | +
|
| 131 | + Данные карточки лежат в obj["data"][0]["categoryData"]["product"] |
| 132 | + """ |
| 133 | + |
| 134 | + real_url = "https://fix-price.com/catalog/" |
| 135 | + if url is None: |
| 136 | + if category is None or product_id is None or slug is None: |
| 137 | + raise TypeError("Either url or (category, product_id, slug) must be provided") |
| 138 | + |
| 139 | + real_url += f"{category}/p-{product_id}-{slug}" |
| 140 | + else: |
| 141 | + real_url += url |
| 142 | + |
| 143 | + page = await self._parent.ctx.new_page() |
| 144 | + await page.goto(real_url, wait_until="domcontentloaded") |
| 145 | + |
| 146 | + raw_json = await page.evaluate(""" |
| 147 | + () => { |
| 148 | + const marker = "window.__NUXT__="; |
| 149 | +
|
| 150 | + for (const s of document.scripts) { |
| 151 | + const txt = s.textContent || ""; |
| 152 | + const idx = txt.indexOf(marker); |
| 153 | +
|
| 154 | + if (idx !== -1) { |
| 155 | + let expr = txt.slice(idx + marker.length).trim(); |
| 156 | +
|
| 157 | + if (expr.endsWith(";")) { |
| 158 | + expr = expr.slice(0, -1); |
| 159 | + } |
| 160 | +
|
| 161 | + const obj = Function('"use strict"; return (' + expr + ')')(); |
| 162 | + return JSON.stringify(obj); |
| 163 | + } |
| 164 | + } |
| 165 | +
|
| 166 | + return null; |
| 167 | + } |
| 168 | + """) |
| 169 | + nuxt_data = json.loads(raw_json)["data"][0]["categoryData"]["product"] if raw_json else None |
| 170 | + |
| 171 | + await page.close() |
| 172 | + |
| 173 | + return nuxt_data |
0 commit comments