-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalscraper.py
417 lines (351 loc) · 13.4 KB
/
malscraper.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# Copyright [2022] [ROCHIT]
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
from config import *
from requests import get
from bs4 import BeautifulSoup
import os
import re
import math
import random
import time
import pandas as pd
import sqlite3
DB = (DB_win if os.name == 'nt' else DB_unix)
# interface with SQL
def run_query(DB, q):
with sqlite3.connect(DB) as conn:
return pd.read_sql(q, conn)
def run_cmd(DB, c):
with sqlite3.connect(DB) as conn:
conn.execute('PRAGMA foreign_key = 1;')
conn.isolation_level = None
conn.execute(c)
def run_insert(DB, c, val):
with sqlite3.connect(DB) as conn:
conn.execute('PRAGMA foreign_keys = 1;')
conn.isolation_level = None
conn.execute(c, val)
# Studios Scraper
def studios_scrape(DB=DB):
start_time = time.time()
insert_q = '''
INSERT OR IGNORE INTO studios(
studio_id,
studio_name
)
VALUES (?,?)
'''
# special query for unknown studio
insert_sp = '''
INSERT OR IGNORE INTO studios(
studio_id,
studio_name
)
VALUES (9999,'Unknown')
'''
run_cmd(DB, insert_sp)
# make request
url = 'https://myanimelist.net/anime/producer'
headers = {
"Usr-agent": "mal scraper for research"
}
# handel timeouts
try:
response = get(url, headers=headers, timeout=10)
except:
print("request timeout")
# dump failed query
failed_q = []
# create soup object
html_soup = BeautifulSoup(response.text, 'html.parser')
total_studios = len(html_soup.find_all('a', class_='genre-name-link'))
for i in range(total_studios):
result = html_soup.find_all(
'a', class_='genre-name-link')[i].attrs['href'].replace('/anime/producer/', '').split('/', 1)
studio_id = result[0]
studio_name = result[1]
# Write into SQL DB
try:
run_insert(DB, insert_q, (
int(studio_id),
studio_name
))
except:
print('Insert failed {}'.format(studio_name))
failed_q.append(studio_name)
pass
# provide stats
os.system('cls' if os.name == 'nt' else 'clear')
print('scraping studio data ')
print('scraping {}'.format(url))
print('inserted into database: \'{}\''.format(studio_name))
print('scraping complete')
print('time elapsed : {:.4f} s'.format(time.time()-start_time))
# Tags Scraper
def tags_scrape(DB=DB):
start_time = time.time()
insert_q = '''
INSERT OR IGNORE INTO tags(
tag_id,
tag_name
)
VALUES (?,?)
'''
# make request
url = 'https://myanimelist.net/anime.php'
headers = {
"Usr-agent": "mal scraper for research"
}
# handel timeouts
try:
response = get(url, headers=headers, timeout=10)
except:
print("request timeout")
# dump failed query
failed_q = []
# create soup object
html_soup = BeautifulSoup(response.text, 'html.parser')
total_tags = len(html_soup.find_all('a', class_='genre-name-link'))
for i in range(total_tags):
res = html_soup.find_all(
'a', class_='genre-name-link')[i].attrs['href']
# check for links
if re.search("^/anime/genre", res):
result = html_soup.find_all(
'a', class_='genre-name-link')[i].attrs['href'].replace('/anime/genre/', '').split('/', 1)
tag_id = result[0]
tag_name = result[1]
# Write in to SQL DB
try:
run_insert(DB, insert_q, (
int(tag_id),
tag_name
))
except:
print('Insert failed {}'.format(tag_name))
failed_q.append(tag_name)
pass
# provide stats
os.system('cls' if os.name == 'nt' else 'clear')
print('scraping studio data ')
print('scraping {}'.format(url))
print('inserted into database: \'{}\''.format(tag_name))
print('scraping complete')
print('time elapsed : {:.4f} s'.format(time.time()-start_time))
def anime_scrape(DB=DB, sleep_min=sleep_min, sleep_max=sleep_max):
start_time = time.time()
insert_q1 = '''
INSERT OR IGNORE INTO animes(
anime_id,
anime_name,
studio_id,
episodes_total,
source_material,
air_date,
overall_rating,
members,
synopsis
)
VALUES (?,?,?,?,?,?,?,?,?)
'''
insert_q2 = '''
INSERT OR IGNORE INTO anime_tags(
anime_id,
tag_id
)
VALUES (?,?)
'''
# make request
url = 'https://myanimelist.net/anime.php'
headers = {
"Usr-agent": "mal scraper for research"
}
# handel timeouts
try:
response = get(url, headers=headers, timeout=10)
except:
print("request timeout")
# dump failed query
failed_q = []
# create soup object
html_soup_initial = BeautifulSoup(response.text, 'html.parser')
total_tags = html_soup_initial.find_all('a', class_='genre-name-link')
requests = 0
for i in range(len(total_tags)):
tag_details = total_tags[i]
res = total_tags[i].attrs['href']
if re.search("^/anime/genre", res):
total_animes = int(tag_details.text.split(
'(')[-1].replace(')', '').replace(',', ''))
link_val = res.replace('/anime/genre/', '').split('/')[0]
for i in range(math.ceil(total_animes/100)):
url = 'https://myanimelist.net/anime/genre/{0}/?page={1}'.format(
link_val, i+1)
headers = {
"Usr-agent": "mal scraper for research"
}
print("Scraping {}".format(url))
# handel timeouts
try:
response = get(url, headers=headers, timeout=10)
except:
print("Request timeout")
pass
if response.status_code != 200:
print("Request: {}; Status code: {}".format(
requests, response.status_code))
pass
html_soup = BeautifulSoup(response.text, 'html.parser')
containers = html_soup.find_all('div', class_='seasonal-anime')
for container in containers:
# primary key for "animes"
anime_id = container.find(
'div', class_='genres js-genre').attrs['id']
# foreign key for "animes"
for r in container.find_all('a'):
x = re.search("^/anime/producer/", r.attrs['href'])
if x:
try:
studio_id = r.attrs['href'].replace(
'/anime/producer/', '').split('/')[0]
except:
studio_id = 9999
# Anime info
anime_name = container.find('a', class_='link-title').text
for r in container.find('div', class_='info').find_all('span'):
if re.search("eps$|ep$", r.text):
ep_totals = r.text
break
for r in container.find_all('div', class_='property'):
if re.search("^Source", r.find('span', class_='caption').text):
src_mat = r.find('span', class_='item').text
break
for r in container.find('div', class_='info').find_all('span', class_='item'):
if re.search("[0-9]$", r.text):
air_dt = int(r.text.split(',')[1])
break
member = container.find_all(
'div', class_='scormem-item')[1].text
synopsis = container.find(
'div', class_='synopsis js-synopsis').find('p', class_='preline').text
try:
orating = float(container.find_all(
'div', class_='scormem-item')[0].text)
except:
orating = 0.0
# write into SQL DB
try:
run_insert(DB, insert_q1, (int(anime_id), anime_name, int(studio_id),
ep_totals, src_mat, air_dt, orating, member.strip(), synopsis))
except Exception as e:
print('Failed to insert into animes for anime_id: {0}, {1}'.format(
anime_id, e))
pass
# Container for anime_tags
anime_tags = []
for r in container.find('div', class_='properties').find_all('a'):
if re.search("^/anime/genre", r.attrs['href']):
anime_tags.append(r)
for tag in anime_tags:
tag_id = tag.attrs['href'].replace(
'/anime/genre/', '').split('/')[0]
for t in tag_id:
try:
run_insert(DB, insert_q2, (
int(anime_id), int(t)))
except Exception as e:
print('Failed to insert into anime_tags for anime_id: {0}, {1}'.format(
anime_id, e))
pass
# provide stats
current_time = time.time()
elapsed_time = current_time - start_time
requests += 1
print(
'Requests Completed: {}; Frequency: {} requests/s'.format(requests, requests/elapsed_time))
print('Elapased Time: {} minutes'.format(elapsed_time/60))
print('Pausing...')
time.sleep(random.uniform(sleep_min, sleep_max))
def review_scrape(DB=DB, pg_start=pg_start, pg_end=pg_end, sleep_min=sleep_min, sleep_max=sleep_max):
start_time = time.time()
insert_q = '''
INSERT OR IGNORE INTO reviews(
review_id,
anime_id,
review_date,
reviewer_rating,
review_tag,
review_body
)
VALUES (?,?,?,?,?,?)
'''
requests = 0
for j in range(pg_start, pg_end):
url = 'https://myanimelist.net/reviews.php?t=anime&spoiler=on&p={}'.format(
j)
headers = {
"Usr-agent": "mal scraper for research"
}
print("Scraping: {}".format(url))
# handel timeout
try:
response = get(url, headers=headers, timeout=10)
except:
print("Request timeout")
pass
if response.status_code != 200:
print("Request: {}; Status Code: {}".format(
requests, response.status_code))
pass
html_soup = BeautifulSoup(response.text, 'html.parser')
review_containers = html_soup.find_all(
'div', class_='review-element js-review-element')
for container in review_containers:
review_element = container.div
# review id Primary key
r_id = int(container.find('div', class_='open').find(
'a').attrs['href'].replace('https://myanimelist.net/reviews.php?id=', ''))
# anime id
a_id = int(container.find('div', class_='titleblock mb4').find(
'a', class_='title ga-click').attrs['href'].replace('https://myanimelist.net/anime/', '').split('/')[0])
# review info
r_date = container.find('div', class_='update_at').text
r_rate = int(container.find('div', class_='rating').find(
'span', class_='num').text)
r_tag = container.find('div', class_='js-btn-label').text.strip()
r_body = container.find(
'div', class_='text').text.strip()
# write into SQL DB
try:
run_insert(DB, insert_q, (r_id, a_id,
r_date, r_rate, r_tag, r_body))
except Exception as e:
print('Failed to scrape anime_id : {} {}'.format(a_id, e))
pass
# Provide stats for monitoring
current_time = time.time()
elapsed_time = current_time - start_time
requests = j + 1 - pg_start
print('Requests Completed: {}; Frequency: {} requests/s'.format(requests,
requests/elapsed_time))
print('Elapased Time: {} minutes'.format(elapsed_time/60))
if requests == pg_end - pg_start + 1:
print('Scrape Complete')
break
print('Pausing...')
time.sleep(random.uniform(sleep_min, sleep_max))
def scrape_all():
studios_scrape() # 35secs
tags_scrape() # 10secs
print('Halting...')
time.sleep(random.uniform(sleep_min, sleep_max))
os.system('cls' if os.name == 'nt' else 'clear')
anime_scrape() # 3 hrs
print('Halting...')
review_scrape() # 3
print('Halting...')
if __name__ == '__main__':
scrape_all()