-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL_DIS2.py
89 lines (78 loc) · 1.69 KB
/
L_DIS2.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
#!/bin/usr/python
#encoding: utf-8
# hphm.txt need encode with utf-8
import os #mkdir
import time
import sys #path
import shutil # rmtree
import cx_Oracle
import imghdr
import urllib
import urllib2
reload(sys)
sys.setdefaultencoding("utf-8") #设置中文文件夹
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
# url save images
def downloadimage(args):
pass
#
def geturl(args):
pass
def mkimages():
if os.path.exists('images'):
shutil.rmtree('images')
print "rm images"
time.sleep(1)
os.mkdir('images')
print "mkdir images"
else:
os.mkdir('images')
print "mkdir images"
#
def start():
mkimages()
cp_in = open('hphm.txt')
lines = cp_in.readlines()
print lines
try:
db = cx_Oracle.connect('si01','jp2011','100.11.44.237/SICSDB')
for line in lines:
hphm = line.strip()
cphm = line[3:9]
# print cphm
path = 'images/%s'%hphm
#path = unicode(path,"GB2312")# chinese code problem
if os.path.exists(path.encode('gb2312')):
continue
else:
os.mkdir(path.encode('gb2312'))
py = 0
i = 0
while i <= 10:
cursor = db.cursor()
sql = 'select * from car_tab_temp where HPHM = ' + '\'' + hphm + '\''
# print sql
cursor.execute(sql)
results = cursor.fetchall()
try:
url = results[i+py][11]
print url
except Exception,e:
break
try:
response = urllib2.urlopen(url,timeout = 1)
webpage = response.read()
print imghdr.what('',webpage)
print 'ok'
urllib.urlretrieve(url,u'%s/%d.jpg'%(path,i))
print "image saved"
i = i + 1
except Exception,e:
print 'timeout'
py = py + 1
except Exception,e:
print 'Some worry happend' , e
finally:
db.close()
cp_in.close()
start()