From 4656666a6c446d85113425f551b133ab27648376 Mon Sep 17 00:00:00 2001 From: Neko Aria <990879119@qq.com> Date: Sun, 19 Jun 2022 19:59:32 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20ascii2d=20=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=20`url`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PicImageSearch/ascii2d.py | 8 +------- PicImageSearch/model/ascii2d.py | 13 ++++++++++--- demo/demo_ascii2d.py | 1 + 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/PicImageSearch/ascii2d.py b/PicImageSearch/ascii2d.py index d4b1ac5b..eb7b53cf 100644 --- a/PicImageSearch/ascii2d.py +++ b/PicImageSearch/ascii2d.py @@ -24,12 +24,6 @@ def __init__(self, bovw: bool = False, **request_kwargs: Any): super().__init__(**request_kwargs) self.bovw: bool = bovw - @staticmethod - def _slice(resp: str) -> Ascii2DResponse: - utf8_parser = HTMLParser(encoding="utf-8") - d = PyQuery(fromstring(resp, parser=utf8_parser))("div.row.item-box") - return Ascii2DResponse(d) - async def search( self, url: Optional[str] = None, file: Optional[BinaryIO] = None ) -> Ascii2DResponse: @@ -63,4 +57,4 @@ async def search( if self.bovw: resp = await self.get(str(resp.url).replace("/color/", "/bovw/")) - return self._slice(resp.text) + return Ascii2DResponse(resp) diff --git a/PicImageSearch/model/ascii2d.py b/PicImageSearch/model/ascii2d.py index 9930794c..c451d445 100644 --- a/PicImageSearch/model/ascii2d.py +++ b/PicImageSearch/model/ascii2d.py @@ -1,5 +1,7 @@ from typing import Dict, List +from httpx import Response +from lxml.html import HTMLParser, fromstring from pyquery import PyQuery @@ -48,7 +50,12 @@ def _get_info(data: PyQuery) -> Dict[str, str]: class Ascii2DResponse: - def __init__(self, data: PyQuery): - self.origin: PyQuery = data # 原始数据 + def __init__(self, resp: Response): + self.origin: str = resp.text # 原始数据 + utf8_parser = HTMLParser(encoding="utf-8") + data = PyQuery(fromstring(self.origin, parser=utf8_parser)) # 结果返回值 - self.raw: List[Ascii2DItem] = [Ascii2DItem(i) for i in data.items()] + self.raw: List[Ascii2DItem] = [ + Ascii2DItem(i) for i in data.find("div.row.item-box").items() + ] + self.url: str = str(resp.url) diff --git a/demo/demo_ascii2d.py b/demo/demo_ascii2d.py index d357a25c..74c97318 100644 --- a/demo/demo_ascii2d.py +++ b/demo/demo_ascii2d.py @@ -32,6 +32,7 @@ def test_sync() -> None: def show_result(resp: Ascii2DResponse) -> None: # logger.info(resp.origin) # 原始数据 + logger.info(resp.url) # 搜索结果链接 logger.info(resp.raw[1].origin) logger.info(resp.raw[1].thumbnail) logger.info(resp.raw[1].title)