/
ExceptionOptions.py
73 lines (64 loc) · 1.94 KB
/
ExceptionOptions.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
import re
import os
from PersonalThemeSearch import BlogStyleDetection
import Tumblrimage
from urllib.parse import quote
import urllib.request
import urllib
import traceback
def getHtml(url):
url = quote(url, safe='/:?=')
try:
page = urllib.request.urlopen(url)
html = page.read().decode('utf-8')
return html
except:
# traceback.print_exc()
print('The URL you requested could not be found in Module image')
return 'Html'
def ImgDownloadException(url):
html = getHtml(url)
reg = '<img src="(.*.(jpg))"'
imgre = re.compile(reg)
imglist_none = re.findall(imgre, html)
imglist = list(set(imglist_none))
if imglist and not BlogStyleDetection(url):
PrePostname = Tumblrimage.getPostname(url)
txt = re.search('/', PrePostname)
if txt:
Postnames = PrePostname.split('/')
Postname = Postnames[0]
# print(PrePostname,Postnames)
else:
Postname = PrePostname
print(len(imglist))
print(imglist)
i = 0
path = 'Tumblrimgdownload/'
if not os.path.exists(path):
os.makedirs(path)
for imgurls in imglist:
Name = Postname + '_' + str(i)
imgurl = imgurls[0]
Postfix = imgurls[1]
target = path + '%s.%s' % (Name, Postfix)
i += 1
loop(imgurl,target)
return True
else:
print('There is no image!')
return False
def loop(imgurl,target):
try:
print("Downloading %s " % target)
urllib.request.urlretrieve(imgurl, target)
except urllib.error.URLError as ex: #处理超时、url不正确异常
print("Connecting error:%s"% ex)
# loop(imgurl, target)
print('The image is lost.')
if __name__ == '__main__':
select = 'Y'
while (select == 'Y'):
URL = input('Input url: ')
ImgDownloadException(URL)
select = input("Do you want to Continue? [Y/N]")