-
Notifications
You must be signed in to change notification settings - Fork 2
/
SFacgSpider.py
224 lines (209 loc) · 7.94 KB
/
SFacgSpider.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# -*- coding: UTF-8 -*-
def save(filename, contents):
fh = open(filename, 'w', encoding='utf-8')
fh.write(contents)
fh.close()
def saveimg(filename, contents):
fh = open(filename, 'wb')
fh.write(contents)
fh.close()
def sfnext(url):
import re,urllib
html=urllib.request.urlopen(url).read().decode("utf-8")
xyz1 =re.search('class="btn normal">下一章</a>', html, flags=0).span()
xyz1=xyz1[0]
xyz=html[:xyz1]
xyz=xyz[-40:]
xyz2=re.search('=',xyz,flags=0).span()
xyz2=xyz2[1]
xyz=xyz[xyz2:]
xyz=xyz[1:][:-2]
xyz="http://book.sfacg.com"+xyz
return xyz
def sflast(url):
import re,urllib
html=urllib.request.urlopen(urllib.request.Request(url)).read().decode("utf-8")
syz1 = re.search('class="btn normal">上一章</a>', html, flags=0 ).span()
syz1=syz1[0]
syz=html[:syz1]
syz=syz[-200:]
syz2=re.search('<a href=',syz,flags=0).span()
syz2=syz2[1]
syz=syz[syz2:]
syz=syz[1:][:-2]
syz="http://book.sfacg.com"+syz
return syz
def sf(url):
import requests
import os
cookies = {
}
headers = {
}
html_file=requests.get(url, headers=headers, cookies=cookies)
html=html_file.text
try:
All_num_1 = re.search('<div class="article" id="article">', html, flags=0).span()
All_num_1 = All_num_1[1]
all = html[All_num_1:]
All_num_2 = re.search('<!-- 按钮 -->', all, flags=0).span()
All_num_2 = All_num_2[0]
all = all[:All_num_2]
Inside_num_1 = re.search('<div class="article-hd">', all, flags=0).span()
Inside_num_1 = Inside_num_1[1]
Inside = all[Inside_num_1:]
Inside_num_2 = re.search('</div>', Inside, flags=0).span()
Inside_num_2 = Inside_num_2[0]
Inside = Inside[:Inside_num_2]
Title_num_1 = re.search('<h1 class="article-title">', Inside, flags=0).span()
Title_num_1 = Title_num_1[1]
Title = Inside[Title_num_1:]
Title_num_2 = re.search('</h1>', Title, flags=0).span()
Title_num_2 = Title_num_2[0]
Title = Title[:Title_num_2]
title_without_md = Title
Title = "# " + Title
Author_num_1 = re.search('作者', Inside, flags=0).span()
Author_num_1 = Author_num_1[1]
Author = Inside[Author_num_1:]
Author_num_2 = re.search('</span>', Author, flags=0).span()
Author_num_2 = Author_num_2[0]
Author = Author[:Author_num_2]
Author = "\n"+"## 作者" + Author
UPDate_time_1 = re.search('更新时间', Inside, flags=0).span()
UPDate_time_1 = UPDate_time_1[1]
UPDate_time = Inside[UPDate_time_1:]
UPDate_time_2 = re.search('</span>', UPDate_time, flags=0).span()
UPDate_time_2 = UPDate_time_2[0]
UPDate_time = UPDate_time[:UPDate_time_2]
UPDate_time = "\n"+"#### 更新时间" + UPDate_time
Count_num_1 = re.search('字数', Inside, flags=0).span()
Count_num_1 = Count_num_1[1]
Count_num = Inside[Count_num_1:]
Count_num_2 = re.search('</span>', Count_num, flags=0).span()
Count_num_2 = Count_num_2[0]
Count_num = Count_num[:Count_num_2]
Count_num = "\n"+"#### 字数" + Count_num
a = re.search('<div class="article-content font16" id="ChapterBody" data-class="font16">', all, flags=0).span()
b = a[1]
c = all[b:]
d = re.search('</div>', c, flags=0).span()
e = d[0]
f = c[:e]
bb = re.sub("</p>", "\n", f, count=0, flags=0)
bb = bb + " "
bb = re.sub("<p>", " ", bb, count=0, flags=0)
bb = bb.replace('\r\n', '')
bb = re.sub("<br>", "", bb, count=0, flags=0)
try:
mg = re.search("<img src='", bb,flags=0).span()
mgg=mg[1]
mggg=bb[mgg:]
mg2 = re.search("' />", mggg,flags=0).span()
mg22=mg2[0]
mgf=mggg[:mg22]
print("文章内图像:"+mgf)
imgys="<img src='"+mgf+"' />"
os.system('wget -P img/ '+mgf)
bb=bb.replace(imgys, '![img1](img/'+mgf[-40:]+')\n')
except:
print("无img")
try:
mg = re.search("<img src='", bb,flags=0).span()
mgg=mg[1]
mggg=bb[mgg:]
mg2 = re.search("' />", mggg,flags=0).span()
mg22=mg2[0]
mgf=mggg[:mg22]
print("文章内图像:"+mgf)
imgys="<img src='"+mgf+"' />"
os.system('wget -P img/ '+mgf)
bb=bb.replace(imgys, '![img2](img/'+mgf[-40:]+')\n')
except:
print("无img")
try:
mg = re.search("<img src='", bb,flags=0).span()
mgg=mg[1]
mggg=bb[mgg:]
mg2 = re.search("' />", mggg,flags=0).span()
mg22=mg2[0]
mgf=mggg[:mg22]
print("文章内图像:"+mgf)
imgys="<img src='"+mgf+"' />"
os.system('wget -P img/ '+mgf)
bb=bb.replace(imgys, '![img3](img/'+mgf[-40:]+')\n')
except:
print("无img")
try:
mg = re.search("<img src='", bb,flags=0).span()
mgg=mg[1]
mggg=bb[mgg:]
mg2 = re.search("' />", mggg,flags=0).span()
mg22=mg2[0]
mgf=mggg[:mg22]
print("文章内图像:"+mgf)
imgys="<img src='"+mgf+"' />"
os.system('wget -P img/ '+mgf)
bb=bb.replace(imgys, '![img4](img/'+mgf[-40:]+')\n')
except:
print("无img")
try:
mg = re.search("<img src='", bb,flags=0).span()
mgg=mg[1]
mggg=bb[mgg:]
mg2 = re.search("' />", mggg,flags=0).span()
mg22=mg2[0]
mgf=mggg[:mg22]
print("文章内图像:"+mgf)
imgys="<img src='"+mgf+"' />"
os.system('wget -P img/ '+mgf)
bb=bb.replace(imgys, '![img5](img/'+mgf[-40:]+')\n')
except:
print("无img")
try:
u=re.search("<img id='vipImage' src='/ajax/ashx/common.ashx?",bb).span()
bbv=u[1]
bbvv=bb[bbv:]
n=re.search("' />",bbvv).span()
bvvv=n[0]
bvv=bbvv[:bvvv]
picurl='http://book.sfacg.com/ajax/ashx/common.ashx'+bvv
vimgys="<img id='vipImage' src='/ajax/ashx/common.ashx"+bvv+"' />"
pi=requests.get(picurl, headers=headers, cookies=cookies).content
saveimg("vimg/"+title_without_md+".gif", pi)
os.system("ffmpeg -i "+"\""+"vimg/"+title_without_md+".gif\" "+"\""+"vimg/"+title_without_md+".png\"")
bb=bb.replace(vimgys, '![vimg](vimg/'+title_without_md+'.png)\n')
os.system("rm \"vimg/"+title_without_md+".gif\"")
ret=Title+Author+UPDate_time+Count_num+"\n***\n"
ret=ret+" "+bb
save(title_without_md+".md", ret)
return ret
except:
print("无VIPimg")
ret=Title+Author+UPDate_time+Count_num+"\n***\n"
ret=ret+" "+bb
save(title_without_md+".md", ret)
return ret
except:
return "获取失败"
def manual(url):
while 1:
print(sf(url))
tj = input("是否继续? ,下一章输入n,上一章输入l,重输链接输入r ")
if tj == "n":
url=sfnext(url)
if tj == "l":
url=sflast(url)
if tj == "r":
url=input("请输入SF文章链接:")
def auto(url):
while 1:
print(sf(url))
url=sfnext(url)
import re,os
url=input("请输入SF文章链接:")
mode=input("模式选择:手动模式输入m 自动模式输入a ")
if mode == "m":
manual(url)
if mode == "a":
auto(url)