-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_image_database.py
67 lines (59 loc) · 2.01 KB
/
get_image_database.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
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
import time
import thread
def download_imag(number):
global img_flag,succes_flag
print('save numner: ' + str(number) + ' img, img_flag = ' + str(img_flag))
requests.packages.urllib3.disable_warnings()
URL = "https://www.db.yugioh-card.com/yugiohdb/card_search.action?request_locale=ja&ope=2&cid=" + str(number)
res = requests.get(str(URL), verify=False, cookies=None)
content = res.content
soup = BeautifulSoup(content.decode('utf-8','ignore'),"html.parser")
#--parse image enc--
tmp_soup = str(soup)
start = tmp_soup.find('&enc')
image_enc = tmp_soup[start:start+27]
#-------------------
#set url
referer_url = 'https://www.db.yugioh-card.com/yugiohdb/card_search.action?ope=2&cid=' + str(number) + '&request_locale=ja'
img_url = 'https://www.db.yugioh-card.com/yugiohdb/get_image.action?type=2&cid=' + str(number) + '&ciid='+str(img_flag)+'&request_locale=ja' + image_enc
print(img_url)
#set request
s = requests.Session()
s.headers.update({'referer': referer_url})
r = s.get(img_url)
#try search error message
soup = BeautifulSoup(r.content,"lxml")
noscript = soup.find_all('noscript')
#set error message
try:
message = noscript[0].string
except:
message = 'aava'
#if no error message, save img
if(message[0] != 'J'):
if(number<10000):
number = "%05d" % int(number)
filename = "img/" + str(number) + '_' + str(img_flag) + ".jpg"
pic_out = file(filename,'w')
pic_out.write(r.content)
pic_out.close()
img_flag+=1
time.sleep(1)
else:
print("ok")
succes_flag = False
img_flag = 1
def download_all(number):
global succes_flag
succes_flag = True
while(succes_flag):
download_imag(number)
if __name__ == "__main__":
succes_flag = True
img_flag = 1
#number range 4007~14000
for i in range(4007,14000):
download_all(i)