-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(baidu): 重构搜索结果的获取和解析逻辑,及返回的结果样式 (close #35)
- Loading branch information
Showing
3 changed files
with
24 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,19 @@ | ||
import json | ||
import re | ||
from typing import Any, Dict, List, Optional | ||
from typing import Any, Dict, List | ||
|
||
|
||
class BaiDuItem: | ||
def __init__(self, data: Dict[str, Any]): | ||
self.origin: Dict[str, Any] = data # 原始数据 | ||
self.page_title: str = data["fromPageTitle"] # 页面标题 | ||
self.title: str = data["title"][0] # 标题 | ||
self.abstract: str = data["abstract"] # 说明文字 | ||
self.image_src: str = data["image_src"] # 图片地址 | ||
self.url: str = data["url"] # 图片所在网页地址 | ||
self.img_list: List[str] = data.get("imgList", []) # 其他图片地址列表 | ||
self.similarity: float = float(f"{float(data['simi']) * 100:.2f}") | ||
self.title: str = data["fromPageTitle"] # 页面标题 | ||
self.thumbnail: str = data["thumbUrl"] # 图片地址 | ||
self.url: str = data["fromUrl"] # 图片所在网页地址 | ||
|
||
|
||
class BaiDuResponse: | ||
def __init__(self, resp_text: str, resp_url: str): | ||
def __init__(self, resp_json: Dict[str, Any], resp_url: str): | ||
self.url: str = resp_url # 搜索结果地址 | ||
self.similar: List[Dict[str, Any]] = [] # 相似结果返回值 | ||
self.raw: List[BaiDuItem] = [] # 来源结果返回值 | ||
# 原始数据 | ||
self.origin: List[Dict[str, Any]] = json.loads( | ||
re.search(r"cardData = (.+);window\.commonData", resp_text)[1] # type: ignore | ||
) | ||
self.same: Optional[Dict[str, Any]] = {} | ||
for i in self.origin: | ||
setattr(self, i["cardName"], i) | ||
if self.same: | ||
self.raw = [BaiDuItem(x) for x in self.same["tplData"]["list"]] | ||
info = self.same["extData"]["showInfo"] | ||
del info["other_info"] | ||
for y in info: | ||
for z in info[y]: | ||
try: | ||
self.similar[info[y].index(z)][y] = z | ||
except IndexError: | ||
self.similar.append({y: z}) | ||
# 获取所有卡片名 | ||
self.item: List[str] = [ | ||
attr | ||
for attr in dir(self) | ||
if not callable(getattr(self, attr)) | ||
and not attr.startswith(("__", "origin", "raw", "same", "url")) | ||
] | ||
self.origin: Dict[str, Any] = resp_json # 原始数据 | ||
# 来源结果返回值 | ||
self.raw: List[BaiDuItem] = [BaiDuItem(i) for i in resp_json["data"]["list"]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters