-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
191 lines (156 loc) · 6.44 KB
/
Copy pathsearch.py
File metadata and controls
191 lines (156 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from typing import ClassVar, Literal, Protocol, Self
from bs4 import Tag
from pydantic import BaseModel, Field
from utils import parse_int
FENLEI_SELECTOR = '.main3>.left>div:has(img[src="../img/search/{fenlei}.png"])'
SHIWEN_BEFORE_SELECTOR = 'img[src="../img/book/shiwen.png"]'
MINGJU_BEFORE_SELECTOR = 'img[src="../img/book/mingjuBefor.png"]'
type SearchType = Literal['shiwen', 'mingju', 'book', 'author']
class Zhuanti(BaseModel):
selector: ClassVar[str] = '.zhuanti-item'
href: str | None = None
title: str | None = None
content: str | None = None
@classmethod
def from_tag(cls, tag: Tag) -> Self:
zhuanti = cls()
if timu := tag.select_one('.timu'):
if (timu.parent and 'href' in timu.parent.attrs
and type(timu.parent.attrs['href']) == str):
zhuanti.href = timu.parent.attrs['href']
zhuanti.title = timu.text.strip()
if content := tag.select_one('.content'):
zhuanti.content = content.text.strip()
return zhuanti
class Shiwen(BaseModel):
selector: ClassVar[str] = '.zongheShiwen'
href: str | None = None
title: str | None = None
source: str | None = None
content: list[str] | None = None
@classmethod
def from_tag(cls, tag: Tag) -> Self:
shiwen = cls()
if timu := tag.select_one('.timu'):
if (timu.parent and 'href' in timu.parent.attrs
and type(timu.parent.attrs['href']) == str):
shiwen.href = timu.parent.attrs['href']
shiwen.title = timu.text.strip()
if source := tag.select_one('.source'):
shiwen.source = source.text.strip()
if contson := tag.select_one('.contson'):
shiwen.content = [
tag.text.strip()
for tag in contson.select('p') or [contson]
]
return shiwen
class Mingju(BaseModel):
selector: ClassVar[str] = '.mingju-item'
href: str | None = None
content: str | None = None
source: str | None = None
source_href: str | None = None
@classmethod
def from_tag(cls, tag: Tag) -> Self:
mingju = cls()
if content := tag.select_one('.mingjuContent'):
if 'href' in content.attrs and type(content.attrs['href']) == str:
mingju.href = content.attrs['href']
mingju.content = content.text.strip()
if source := tag.select_one('.mingjuSource'):
if 'href' in source.attrs and type(source.attrs['href']) == str:
mingju.source_href = source.attrs['href']
mingju.source = source.text.strip()
return mingju
class Book(BaseModel):
selector: ClassVar[str] = '.zongheShiwen'
href: str | None = None
title: str | None = None
alias: str | None = None
mingju_count: int | None = None
tags: set[str] = Field(default_factory=set)
@classmethod
def from_tag(cls, tag: Tag) -> Self:
book = cls()
if timu := tag.select_one('.timu'):
if (timu.parent and 'href' in timu.parent.attrs
and type(timu.parent.attrs['href']) == str):
book.href = timu.parent.attrs['href']
book.title = timu.text.strip()
if beiming := tag.select_one('.bieming2'):
book.alias = beiming.text.strip()
if (img := tag.select_one(MINGJU_BEFORE_SELECTOR)) and img.parent:
if count := parse_int(mingju_count := img.parent.text.strip()):
book.mingju_count = count
if parent := img.parent.parent:
book.tags = set(
text
for child in parent.children
if (text := child.text.strip())
and not text == mingju_count
)
return book
class Author(BaseModel):
selector: ClassVar[str] = '.zongheShiwen'
href: str | None = None
name: str | None = None
avatar: str | None = None
content: str | None = None
shiwen_count: int | None = None
mingju_count: int | None = None
@classmethod
def from_tag(cls, tag: Tag) -> Self:
author = cls()
if avatar := tag.select_one('img'):
if 'src' in avatar.attrs and type(avatar.attrs['src']) == str:
author.avatar = avatar.attrs['src']
if 'alt' in avatar.attrs and type(avatar.attrs['alt']) == str:
author.name = avatar.attrs['alt']
if (a := avatar.parent) and 'href' in a.attrs and type(a.attrs['href']) == str:
author.href = a.attrs['href']
if (img := tag.select_one(SHIWEN_BEFORE_SELECTOR)):
if img.parent and (count := parse_int(img.parent.text.strip())):
author.shiwen_count = count
if (img := tag.select_one(MINGJU_BEFORE_SELECTOR)) and img.parent:
if count := parse_int(img.parent.text.strip()):
author.mingju_count = count
if contson := tag.select_one('.contson'):
author.content = contson.text.strip()
return author
class FromTag(Protocol):
selector: ClassVar[str]
@classmethod
def from_tag(cls, tag: Tag) -> Self:
...
def parse_tags[T: FromTag](cls: type[T], tag: Tag) -> list[T]:
return [cls.from_tag(tag) for tag in tag.select(cls.selector)]
ITEM_TYPE_TO_CLASS: dict[Literal[SearchType, "zhuanti"], type[FromTag]] = {
'zhuanti': Zhuanti,
'shiwen': Shiwen,
'mingju': Mingju,
'book': Book,
'author': Author,
}
class SearchResult(BaseModel):
zhuantis: list[Zhuanti] | None = None
shiwens: list[Shiwen] | None = None
mingjus: list[Mingju] | None = None
books: list[Book] | None = None
authors: list[Author] | None = None
more: bool = False
@classmethod
def from_tag(cls, tag: Tag, type: SearchType | None = None):
result = cls()
if type is None:
result.more = tag.select_one('.viewMore') is not None
for field in ITEM_TYPE_TO_CLASS.keys():
selector = FENLEI_SELECTOR.format(fenlei=field)
if fenlei := tag.select_one(selector):
items = parse_tags(ITEM_TYPE_TO_CLASS[field], fenlei)
setattr(result, f'{field}s', items)
return result
if len(zhuantis := parse_tags(Zhuanti, tag)):
result.zhuantis = zhuantis
setattr(result, f'{type}s', parse_tags(ITEM_TYPE_TO_CLASS[type], tag))
result.more = tag.select_one('.amore') is not None
return result