-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaHaSpider.py
321 lines (277 loc) · 12.4 KB
/
BaHaSpider.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import sys, getopt
import random
import time
import requests
from bs4 import BeautifulSoup
import datetime
from CAniDB import CAniDB
# ================================================================
#
# ================================================================
class CAnimateData:
def __init__(self, sn, animate_name, score, score_people):
self.sn = sn
self.animate_name = animate_name
self.score = score
self.score_people = score_people
def export_rank_list(file_name, list_score_rank, list_people_rank):
print("============================================================")
f = open(file_name, "w", encoding='UTF-8')
if f:
f.write("<html>\n\t<head>")
f.write("\n\t\t<meta name=\"viewport\" content=\"width=device-width, user-scalable=yes, initial-scale=1\">")
f.write("\n\t\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">")
f.write("\n\t\t<style>\n\t\t\ttable, th, td {border: 1px solid black; border-collapse: collapse;} \
th, td {padding: 3px;}\n\t\t\t.table_row_odd {background: #F3F4F6;}\n\t\t</style>")
f.write("\n\t\t<title>動畫評分列表 - 巴哈姆特動畫瘋</title>")
f.write("\n\t\t<script type=\"text/javascript\">")
f.write("\n\t\tfunction reload() {")
f.write("\n\t\t\thref = window.location.href.split(\"?time=\");")
f.write("\n\t\t\ttime = (new Date()).getTime();")
f.write("\n\t\t\tif( href[1] == null || ( time - href[1] > 1800000 ) )")
f.write("\n\t\t\t\twindow.location.href = href[0] + \"?time=\" + time;")
f.write("\n\t\t}")
f.write("\n\t\tfunction showPeopleRankList() {")
f.write("\n\t\t\tdocument.getElementById(\"table_score\").style.display = 'none';")
f.write("\n\t\t\tdocument.getElementById(\"table_people\").style.display = '';")
f.write("\n\t\t}")
f.write("\n\t\tfunction showScoreRankList() {")
f.write("\n\t\t\tdocument.getElementById(\"table_people\").style.display = 'none';")
f.write("\n\t\t\tdocument.getElementById(\"table_score\").style.display = '';")
f.write("\n\t\t}")
f.write("\n\t\t</script>")
f.write("\n\t</head>\n\t<body onload=\"reload();\">\n\t\t")
f.write(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
# Write Score-Rank Data
f.write("\n\t\t<table id=\"table_score\" style=\"margin:0 auto;\">")
f.write("\n\t\t\t<tr>")
f.write("\n\t\t\t\t<th>排名</th>")
f.write("<th style=\"width:500px;\">動畫名稱</th>")
f.write("<th style=\"background: #C3C3C3;\" onclick=\"showScoreRankList()\">評分 ∇</th>")
f.write("<th onclick=\"showPeopleRankList()\">評分人數</th>")
f.write("\n\t\t\t</tr>")
odd = True
for idxData, data in enumerate(list_score_rank):
if odd:
f.write("\n\t\t\t<tr class=\"table_row_odd\">")
else:
f.write("\n\t\t\t<tr>")
f.write("\n\t\t\t\t<td>" + str(idxData + 1) + "</td>")
f.write("<td style=\"text-align:center;\"><a href=\"" + urlBase + str(
data.sn) + "\" target=\"_blank\">" + data.animate_name + "</a></td>")
f.write("<td style=\"text-align:center;\">" + str(data.score) + "</td>")
f.write("<td style=\"text-align:center;\">" + str(data.score_people) + "</td>")
f.write("\n\t\t\t</tr>")
odd = not odd
f.write("\n\t\t</table>")
# Write People-Rank Data
f.write("\n\t\t<table id=\"table_people\" style=\"margin:0 auto;display:none;\">")
f.write("\n\t\t\t<tr>")
f.write("\n\t\t\t\t<th>排名</th>")
f.write("<th style=\"width:500px;\">動畫名稱</th>")
f.write("<th onclick=\"showScoreRankList()\">評分</th>")
f.write("<th style=\"background: #C3C3C3;\" onclick=\"showPeopleRankList()\">評分人數 ∇</th>")
f.write("\n\t\t\t</tr>")
odd = True
for idxData, data in enumerate(list_people_rank):
if odd:
f.write("\n\t\t\t<tr class=\"table_row_odd\">")
else:
f.write("\n\t\t\t<tr>")
f.write("\n\t\t\t\t<td>" + str(idxData + 1) + "</td>")
f.write("<td style=\"text-align:center;\"><a href=\"" + urlBase + str(
data.sn) + "\" target=\"_blank\">" + data.animate_name + "</a></td>")
f.write("<td style=\"text-align:center;\">" + str(data.score) + "</td>")
f.write("<td style=\"text-align:center;\">" + str(data.score_people) + "</td>")
f.write("\n\t\t\t</tr>")
odd = not odd
f.write("\n\t\t</table>")
f.write(
"\n\t\t<nobr><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td><a href=\"https://www.free-counter.jp/\"><img src=\"https://www.f-counter.net/ani1/48/1644728724/\" alt=\"カウンター\" border=\"0\" style=\"margin:0px; padding:0px; border:0px; vertical-align:bottom\"></a></td>")
f.write(
"\n\t\t<td><a href=\"https://www.free-counter.jp/\"><img src=\"https://www.f-counter.net/ani2/48/1644728724/\" alt=\"カウンター\" border=\"0\" style=\"margin:0px; padding:0px; border:0px; vertical-align:bottom\"></a></td>")
f.write("\n\t\t</tr></tbody></table></nobr>")
f.write("\n\t</body>\n</html>")
f.close()
# ================================================================
# Global Declaration
# ================================================================
totalSN = 34000
name = ''
num = 0.0
people = 0
recrawl = False
listSN = []
listScoreRank = []
listPeopleRank = []
urlBase = "https://ani.gamer.com.tw/animeVideo.php?sn="
oAniDB = CAniDB()
delay_choices = [0.2, 0.5, 0.5, 1, 1, 1, 1.2, 1.2, 1.5, 1.5, 2, 3] # 延遲的秒數
def is_float(element: any) -> bool:
if element is None:
return False
try:
float(element)
return True
except ValueError:
return False
# ================================================================
#
# ================================================================
# Read arguments
argv = sys.argv[1:]
try:
opts, args = getopt.getopt(argv, "hr:", ["help", "recrawl="])
except getopt.GetoptError:
print('BaHaSpider.py [options]')
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
print("BaHaSpider.py [options]\n")
print("General Options:")
print(" -h, --help Show help.")
print(" -r, --recrawl <numbers> Re-crawl all data from ani.gamer.com.tw")
print(" and specify the total quantity you want to crawl.")
sys.exit()
elif opt in ("-r", "--recrawl"):
recrawl = True
totalSN = int(arg)
# Create DB
oAniDB.create_table_animate_list()
oAniDB.create_table_episode()
#
if recrawl:
maxSN = 0
else:
maxSN = oAniDB.get_max_sn()
if maxSN >= totalSN:
totalSN = maxSN + 300
for i in range(max(1, (maxSN + 1)), totalSN):
listSN.append(str(i))
# ================================================================
# Crawl existing animate in DB
# ================================================================
if not recrawl:
listDBSN = oAniDB.query_animate_sn_list()
i = 0
lenSN = len(listDBSN)
while len(listDBSN) != 0:
row = listDBSN.pop(0)
sn = row[0]
url = urlBase + str(sn)
# Get HTML
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")
delay = random.choice(delay_choices) # 隨機選取秒數
time.sleep(delay)
# Get Data
divs = soup.select(".anime_name")
if len(divs) == 0:
continue
for div_name in divs:
name = div_name.select_one("h1").getText()
names = name.split(" [")
name = names[0]
for index, strName in enumerate(names):
if index == 0:
continue
if not strName[0].isnumeric():
if strName.find("特別篇]") > -1:
continue
else:
name += " [" + strName
divs = soup.select(".score-overall-number")
for div_num in divs:
num = div_num.getText()
divs = soup.select(".score-overall-people")
for div_people in divs:
people = div_people.getText().replace(',', '')
people = people.replace("人評價", "")
# Update Data to DB
if (num.isnumeric() or is_float(num)) and people.isnumeric():
rowid = oAniDB.update_animate(name, str(sn), float(num), int(people))
# Update Animate Episode
divs = soup.select(".season")
for div in divs:
lis = div.select("li")
for li in lis:
ep = li.select_one("a").getText()
strSN = li.select_one("a").get("href")
strSN = strSN.split("?sn=")
oAniDB.update_episode(rowid, ep, strSN[1])
oAniDB.commit()
# os.system('cls' if os.name=='nt' else 'clear')
i += 1
print("Process: %.2f%% ( sn: %d )" % (float(i)/float(lenSN)*100, sn))
print(name + " / " + str(num) + " / " + str(people) + "人")
# ================================================================
# Crawl new animate on web
# ================================================================
while len(listSN) != 0:
sn = listSN.pop(0)
url = urlBase + sn
# Get HTML
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")
delay = random.choice(delay_choices) # 隨機選取秒數
time.sleep(delay)
# Get Data
divs = soup.select(".anime_name")
if len(divs) == 0:
continue
for div_name in divs:
name = div_name.select_one("h1").getText()
names = name.split(" [")
name = names[0]
for index, strName in enumerate(names):
if index == 0:
continue
if not strName[0].isnumeric():
if strName.find("特別篇]") > -1:
continue
else:
name += " [" + strName
divs = soup.select(".score-overall-number")
for div_num in divs:
num = div_num.getText()
divs = soup.select(".score-overall-people")
for div_people in divs:
people = div_people.getText().replace(',', '')
people = people.replace("人評價", "")
# Update Data to DB
if (num.isnumeric() or is_float(num)) and people.isnumeric():
rowid = oAniDB.update_animate(name, sn, float(num), int(people))
# Remove Duplicated Animate
divs = soup.select(".season")
for div in divs:
lis = div.select("li")
for li in lis:
ep = li.select_one("a").getText()
strSN = li.select_one("a").get("href")
strSN = strSN.split("?sn=")
if int(strSN[1]) == 28423:
print("warn")
if strSN[1] in listSN:
listSN.remove(strSN[1])
oAniDB.update_episode(rowid, ep, strSN[1])
oAniDB.commit()
# os.system('cls' if os.name=='nt' else 'clear')
print("Process: %.2f%% ( %s / %d )" % (float(sn)/float(totalSN)*100, sn, totalSN))
print(sn + ": " + name + " / " + str(num) + " / " + str(people) + "人")
# Query Score-Rank data from DB
listScore = [5.0, 4.9, 4.8, 4.7, 4.6, 4.5, 4.4, 4.3, 4.2, 4.1, 4.0, 3.9, 3.8, 3.7, 3.6, 3.5, 3.4, 3.3, 3.2, 3.1, 3.0,
2.9, 2.8, 2.7, 2.6, 2.5, 2.4, 2.3, 2.2, 2.1, 2.0, 1.9, 1.8, 1.7, 1.6, 1.5, 1.4, 1.3, 1.2, 1.1, 1.0]
while len(listScore) != 0:
score = listScore.pop(0)
rows = oAniDB.query_animates_by_score(score)
for row in rows:
listScoreRank.append(CAnimateData(row[1], row[0], row[2], row[3]))
# Query People-Rank data from DB
rows = oAniDB.query_animates_by_score_people()
for row in rows:
listPeopleRank.append(CAnimateData(row[1], row[0], row[2], row[3]))
# Export Result
export_rank_list("C:\\Users\\Jordan\\我的雲端硬碟\\巴哈動畫瘋\\AniRank.html", listScoreRank, listPeopleRank)
oAniDB.close()