-
Notifications
You must be signed in to change notification settings - Fork 1
/
91hanman-list.py
177 lines (159 loc) · 6.99 KB
/
91hanman-list.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
# -*- coding:UTF-8 -*-
from bs4 import BeautifulSoup
from urllib.request import urlretrieve
import requests
import os
import time
import pdb
import socket
import ssl
def loglist(file,data):
f = open(file,'a')
f.write(data+"\n")
f.close()
def download(url,filename):
socket.setdefaulttimeout(60)
#pdb.set_trace() # 运行到这里会自动暂停
try:
urlretrieve(url = url,filename = filename)
except socket.timeout:
count = 1
while count <= 5:
try:
urlretrieve(url = url,filename = filename)
break
except socket.timeout:
err_info = 'Reloading for %d time'%count if count == 1 else 'Reloading for %d times'%count
print(err_info)
count += 1
if count > 5:
print("downloading picture fialed!")
except:
print('捕捉到其他异常')
if __name__ == '__main__':
list_url = []
base_folder ='F:/py3project/91hanman/'
base_url='https://www.91hanman.com'
for index in range(600):
#url=base_url+'/mh/xe/'+str(index)+'.html'
index+=387
url=base_url+'/book/webBookDetail/'+str(index)#https://www.91hanman.com/book/webBookDetail/270
print(str(index)+base_url)
#url=base_url+'/mh/xe/1.html'
# headers = {
# "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
# "Referer": "http://www.google.com/bot.html"
# }
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
"Referer": "http://www.google.com/bot.html"
}
req = requests.get(url = url,headers = headers,verify=False)
#req.encoding = 'gbk'
#html = req.text
html =req.content.decode('utf-8');
bf = BeautifulSoup(html, 'lxml')
unlock = bf.find('img',class_='un-lock')
print(unlock==None)
if unlock==None :
free=""
else:
free="【收费】"
# print(type(bf.title.text))
# print(bf.title.text)
# if bf.title.text == None:
# continue
loglist(base_folder+"91hanman.txt",url + '=' + free+bf.title.text)
print(url + '=' + bf.title.text)
exit()
#定义一组
manhua_list=['tongshifangke=/mh/xe/147.html','furendeliwu=/mh/xe/142.html','zhengrongzhen=/mh/xe/119.html','dianjishaonian=/mh/xe/118.html','maopaihuizhang=/mh/xe/82.html']
print(len(manhua_list))
for each_manhua_list in manhua_list:
each_manhua_list_info = each_manhua_list.split('=')
base_manga_name=each_manhua_list_info[0]
base_log=base_folder+base_manga_name+".txt"
url = base_url+each_manhua_list_info[1]
print(base_manga_name)
print(url)
# base_manga_name='aishangnanguimi'
# base_log=base_folder+base_manga_name+".txt"
# url = base_url+'/mh/xe/20.html'
if base_manga_name not in os.listdir():
os.makedirs(base_manga_name)
for num in range(1,2):
# if num == 1:
# url = 'view-source:https://www.91hanman.com/book/webBookDetail/69'
# else:
# url = 'http://www.shuaia.net/index_%d.html' % num
#TODO:
#url = base_url+'/mh/xe/20.html'
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
}
ssl._create_default_https_context = ssl._create_unverified_context
req = requests.get(url = url,headers = headers,verify=False)
req.encoding = 'gbk'
html = req.text
bf = BeautifulSoup(html, 'lxml')
targets_url = bf.find_all('div',class_='sections')
for each_url in targets_url:
#pdb.set_trace() # 运行到这里会自动暂停
bf_2 = BeautifulSoup(str(each_url), 'lxml')
[s.extract() for s in bf_2("p")]#去除指定标签
targets_url_2 = bf_2.find_all('a')
for each_url_2 in targets_url_2:
print(each_url_2.get('href'))
print(each_url_2.get_text())
#pdb.set_trace()
loglist(base_log,each_url_2.get_text() + '=' + base_url +each_url_2.get('href'))
list_url.append(each_url_2.get_text() + '=' + base_url +each_url_2.get('href'))
#pdb.set_trace() # 运行到这里会自动暂停
# for each in targets_url:
# list_url.append(each.img.get('alt') + '=' + each.get('href'))
print('连接采集完成')
print(list_url)
print(len(list_url))
count=0
for each_img in list_url:
#pdb.set_trace()
# if(count <= 63):
# print(count)
# continue
count +=1
folder=base_folder+base_manga_name+'/'+str(count)
#pdb.set_trace()
print(os.path.exists(folder));
if os.path.exists(folder) == False:
if folder not in os.listdir():
os.makedirs(folder)
#pdb.set_trace() # 运行到这里会自动暂停
img_info = each_img.split('=')
target_url = img_info[1]
filename = img_info[0]
print('下载:' + filename)
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
}
img_req = requests.get(url = target_url,headers = headers,verify=False)
img_req.encoding = 'utf-8'
img_html = img_req.text
img_bf_1 = BeautifulSoup(img_html, 'lxml')
img_url = img_bf_1.find_all('div', class_='detailcontent1')
img_bf_2 = BeautifulSoup(str(img_url), 'lxml')#str 返回一个字符串 把对象转换字符串
img_bf_3 =img_bf_2.find_all('img')
#img_bf_3 =img_bf_2.find_all('img')
i=0
for each_img_2 in img_bf_3:
i += 1
#img_bf_4 = BeautifulSoup(str(each_img_2), 'lxml')#str 返回一个字符串 把对象转换字符串
img_url = each_img_2.get('src')
# if 'images' not in os.listdir():
# os.makedirs('images')
#print(str(i).zfill(2))
#print(folder+"/"+str(i).zfill(2) + '.jpg')
#pdb.set_trace() # 运行到这里会自动暂停
if os.path.exists(folder+"/"+str(i).zfill(2) + '.jpg') == False:
download(url = img_url,filename = folder+"/"+str(i).zfill(2) + '.jpg')
time.sleep(1)
print('下载完成!')