-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathHTMLParser.py
221 lines (181 loc) · 6.28 KB
/
HTMLParser.py
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# (C) 2019-2020 lifegpc
# This file is part of bili.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from html.parser import HTMLParser
from typing import Tuple, List
HTMLAttrs = List[Tuple[str, str]]
class Myparser(HTMLParser):
"解析B站HTML"
script = 0
videodata = ''
def handle_starttag(self, tag, attrs):
if tag == 'script':
self.script = 1
else:
self.script = 0
def handle_data(self, data):
if self.script == 1 and data[0:24] == "window.__INITIAL_STATE__":
self.videodata = data[25:len(data) - 122]
class Myparser2(HTMLParser):
"解析B站HTML"
script = 0
videodata = ''
def handle_starttag(self, tag, attrs):
if tag == 'script':
self.script = 1
else:
self.script = 0
def handle_data(self, data):
if self.script == 1 and data[0:19] == "window.__playinfo__":
self.videodata = data[20:]
class Myparser3(HTMLParser):
script = 0
videodata = ''
def handle_starttag(self, tag, attrs):
if tag == 'script':
self.script = 1
else:
self.script = 0
def handle_data(self, data):
if self.script == 1 and data[0:24] == "window.__INITIAL_STATE__":
self.videodata = data[25:-122]
class AcfunParser(HTMLParser):
script = 0
videoInfo = ''
def handle_starttag(self, tag, attrs):
if tag == 'script':
self.script = 1
else:
self.script = 0
def handle_data(self, data: str):
if self.script == 1:
slist = data.splitlines(False)
for i in slist:
i = i.strip()
if i.startswith("window.pageInfo"):
self.videoInfo = i[37:]
self.videoInfo = self.videoInfo.rstrip(";")
class AcfunBangumiParser(HTMLParser):
script = 0
bangumiData = ''
bangumiList = ''
def handle_starttag(self, tag, attrs):
if tag == 'script':
self.script = 1
else:
self.script = 0
def handle_data(self, data: str):
if self.script == 1:
slist = data.splitlines(False)
for i in slist:
i = i.strip()
if i.startswith("window.pageInfo"):
self.bangumiData = i[39:]
self.bangumiData = self.bangumiData.rstrip(";")
if i.startswith("window.bangumiList"):
self.bangumiList = i[21:]
self.bangumiList = self.bangumiList.rstrip(";")
class NicoUserParser(HTMLParser):
script = 0
userData = ""
def handle_starttag(self, tag, attrs):
if tag == 'script':
self.script = 1
else:
self.script = 0
def handle_data(self, data: str):
if self.script == 1:
slist = data.splitlines(False)
for i in slist:
i = i.strip()
if i.startswith('user.'):
if self.userData != '':
self.userData += f'\n{i}'
else:
self.userData = i
class NicoVideoInfoParser(HTMLParser):
apiData = ''
def handle_starttag(self, tag, attrs: HTMLAttrs):
if tag == 'div':
eid = ''
for t in attrs:
if t[0] == 'id':
eid = t[1]
break
if eid == 'js-initial-watch-data':
for t in attrs:
if t[0] == 'data-api-data':
self.apiData = t[1]
def handle_startendtag(self, tag, attrs):
self.handle_starttag(tag, attrs)
class NicoDescriptionParser(HTMLParser):
data = ''
style = False
script = False
deepdata = []
deepLevel = 0
def handle_data(self, data: str):
if not self.style and not self.script and self.deepLevel == 0:
self.data += data
elif self.deepLevel > 0:
self.deepdata[-1]["content"] += data
def handle_starttag(self, tag: str, attrs: HTMLAttrs):
if tag == "br":
self.handle_data('\n')
if tag == 'style':
self.style = True
if tag == 'a':
self.deepLevel += 1
t = {"type": "link", "href": "", "content": ""}
for a in attrs:
if a[0] == 'href':
t['href'] = a[1]
self.deepdata.append(t)
if tag == 'script':
self.script = True
def handle_endtag(self, tag: str):
if tag == 'style':
self.style = False
if tag == 'a':
t = self.deepdata.pop()
c = t['href'] if t["href"] == t["content"] or t["content"] == "" else f"{t['content']}({t['href']})"
if self.deepLevel == 1:
self.data += c
elif self.deepLevel > 1:
self.deepdata[-1]["content"] += c
self.deepLevel -= 1
if tag == 'script':
self.script = False
def handle_startendtag(self, tag: str, attrs: HTMLAttrs):
if tag in ['style', 'script']:
return
self.handle_starttag(tag, attrs)
if tag == 'a':
self.handle_endtag('a')
class NicoLiveInfoParser(HTMLParser):
data = ''
def handle_starttag(self, tag, attrs: HTMLAttrs):
if tag == 'script':
eid = ''
for t in attrs:
if t[0] == 'id':
eid = t[1]
break
if eid == 'embedded-data':
for t in attrs:
if t[0] == 'data-props':
self.data = t[1]
def handle_startendtag(self, tag, attrs):
self.handle_starttag(tag, attrs)