-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyImgEH.py
More file actions
182 lines (165 loc) · 5.47 KB
/
pyImgEH.py
File metadata and controls
182 lines (165 loc) · 5.47 KB
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
# for e-hentai.org 批量下载图片
import os
from queue import Queue
import re
import time
import threading
import urllib.request
import urllib.error
import bs4
#replace url to start your own download
url = "https://e-hentai.org/g/1012497/4aae504fe9/"
dirname = os.getcwd()
print_lock = threading.Lock()
data_q = Queue()
MaxThread = 40
def getImgAddr(numpages):
print("Start to get imgURL")
#img addr list
list = []
addrlist = []
for iStr in range(1,numpages+1):
tmpAddr = url+"?p="+str(iStr)
# print(tmpAddr)
addrlist.append(tmpAddr)
for iAddr in addrlist:
headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0",
}
request = urllib.request.Request(url=iAddr, headers=headers)
response = urllib.request.urlopen(request)
raw = response.read().decode('utf-8')
# with urllib.request.urlopen(iAddr) as html:
# raw = html.read()
soup = bs4.BeautifulSoup(raw, "html.parser")
tmp_gdtm = soup.findAll('div', class_='gdtm')
for iGdtm in tmp_gdtm:
tmpUrl = iGdtm.find('a').get('href')
id = j + i
# print(tmpUrl)
list.append(tmpUrl)
print("Get imgURL---------Done")
return list
class urlset:
id = 0
url = url
def __init__(self,id,url):
self.id = id
self.url = url
def mkdir(path):
try:
os.makedirs(path)
except:
pass
return path
def saveImg(imgUrl):
time.sleep(0.1)
with print_lock:
#print(imgset.url)
#imgUrl = imgset.url
imgName = imgUrl.split('-')[-1]
# with urllib.request.urlopen(imgUrl) as html:
# rawurl = html.read()
headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0",
}
request = urllib.request.Request(url=imgUrl, headers=headers)
response = urllib.request.urlopen(request)
rawurl = response.read().decode('utf-8')
try:
soup = bs4.BeautifulSoup(rawurl,"lxml")
tmpimgurl = soup.find(id='i3').find('img').get('src')
extension = "."+tmpimgurl.split('.')[-1]
request = urllib.request.Request(url=tmpimgurl, headers=headers)
response = urllib.request.urlopen(request)
rawimg = response.read()
# with urllib.request.urlopen(tmpimgurl) as imghtml:
# rawimg = imghtml.read()
with open(imgName+extension,'wb') as file:
file.write(rawimg)
print(imgName+extension,"------downloaded!")
except:
print(imgUrl, "------Error!")
pass
def worker():
while True:
tmpUrl = data_q.get()
saveImg(tmpUrl)
data_q.task_done()
#def imglist():
def main():
#socket.setdefaulttimeout(10)
for x in range(MaxThread):
t = threading.Thread(target = worker)
t.daemon = True
t.start()
start = time.time()
#----------------------------------
raw = ""
headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0",
}
request = urllib.request.Request(url=url,headers=headers)
response = urllib.request.urlopen(request)
raw = response.read().decode('utf-8')
# raw = html.read()
# with open("test2.html",'r+',encoding='utf-8') as file:
# raw = file.read()
# #print(raw)
#---------------------------------
#图片文件信息在gm class的div里面
soup = bs4.BeautifulSoup(raw,"html.parser")
all_gm = soup.findAll('div',class_='gm')
tmplist = []
numpages = 0
for i in all_gm:
i.findAll('h1')
if(len(i.findAll('h1')) != 0):
tmplist.append(i)
if(len(tmplist)!=1):
print("Multipul error! Please mail the author")
exit()
else:
infolist = tmplist[0]
#================================= use url last split
tmpStr = url.split('/')[-2]
os.chdir(mkdir(tmpStr))
#=================================
with open("info.html",'w',encoding='utf-8') as file:
file.write("<html>\n<head><title>infomation</title>\n</head>")
file.write("<body><p>download source:")
file.write(url)
file.write("</p>")
filename = infolist.findAll('h1')
#=====================
file.write("<p>file name</p>")
for i in filename:
stri = str(i).replace("<h1","<p")
stri = stri.replace("/h1>","/p>")
file.write(stri)
fileinfo = infolist.find(id='gdd')
file.write("<p>file info</p>")
file.write(str(fileinfo))
strinfo = str(fileinfo)
# print(strinfo)
# 暂时匹配的是整数 size的一般是浮点数 日期则有横杠
pages = re.findall(r"gdt2\">(\d+) ",strinfo)
print("图片数:",int(pages[0]))
numpages = int(pages[0])
taginfo = infolist.find(id='taglist')
file.write("<p>tags</p>")
file.write(str(taginfo))
file.write("</body></html>")
#=====================
#所有页 总页数/40
tmp = numpages/40
if(tmp>int(tmp)):
tmp +=1
all_imgs = getImgAddr(int(tmp))
for iimg in all_imgs:
data_q.put(iimg)
all_imgs.clear()
data_q.join()
print("entire job took:", time.time()-start)
if __name__ == '__main__':
main()