用户:Ryan袁奥查看:3 回复:4 评论:3 创建时间:2019-02-16T14:36:22
from tkinter import *
import requests
class App(Tk,Button,Label,Entry):
def __init__(s,t='tk'):
s.root=Tk()
s.root.title(t)
s.buttons=[]
s.labels=[]
s.entrys=dict()
s.clabel=dict()
def newButton(s,w=0,h=0,t='点我',f='black',\
c=lambda:print("click"),si=TOP):
if not w==0 or h==0:
B_new = Button(s.root,text=t,fg=f,\
command=c,width=w,height=h)
else:
B_new = Button(s.root,text=t,fg=f,\
command=c)
B_new.pack(side=si)
s.buttons.append(B_new)
def newText(s,t="这是一个文本标签",\
f='black',w=0,h=0,si=TOP):
if w==0 or h==0:
L_new = Label(s.root,text=t,fg=f)
else:
L_new = Label(s.root,text=t,fg=f,\
width=w,height=h)
L_new.pack(side=si)
s.labels.append(L_new)
def newChangeText(s,name,f='black',w=0,h=0\
,si=TOP):
var = StringVar()
if w==0 or h==0:
L_new = Label(s.root,textvariable=var\
,fg=f)
else:
L_new = Label(s.root,textvariable=var\
,fg=f,width=w,height=h)
L_new.pack(side=si)
s.clabel[name]=(L_new,var)
def setChange(s,name,newstr):
try:
var=s.clabel[name][1]
except:
raise NameError(\
"There is no text label named {}."\
.format(name))
else:
var.set(newstr)
def newEntry(s,name,w=0,read=0,si=TOP):
if read==0:
read='normal'
else:
read='disabled'
if w==0:
E_new=Entry(s.root,exportselection=0,\
state=read)
else:
E_new=Entry(s.root,exportselection=0,\
state=read,width=w)
E_new.pack(side=si)
s.entrys[name]=E_new
def clearEntry(s,name):
s.entrys[name].delete(0, END)
def setEntry(s,name,strs):
s.clearEntry(name)
s.entrys[name].insert(0,strs)
def getEntry(s,name):
return s.entrys[name].get()
def start(s):
s.root.mainloop()
def clear(s):
for x in s.labels+s.buttons+\
list(s.entrys.values()):
x.destroy()
for x in s.clabel.values():
x[0].destroy()
s.lables=[]
s.buttons=[]
s.entrys=dict()
s.cables=dict()
url=''
app = App()
url_list=['#']
name_list=['#']
con=''
def debug(t="程序员加班中..."):
t2 = Tk()
Lt2 = Label(t2, text=t)
Lt2.pack()
def getText(url):
global app,url_list,name_list,con
response = requests.get(url)
response.encoding = 'ascii'
te=response.text
fd='''content="text/html; charset='''
s=te.find(fd)
le=len(fd)
te=te[s+le:]
w=te.find('\"')
code=te[:w]
del(te)
response.encoding = code
t=response.text
del(response)
t=t[t.find('<div id="content'):\
t.find('<div class="con_show_r')]
t=t.split('<br />')
s=t[0].find('</div>')
t[0]=t[0][s+6:]
return t
def tnext():
global app,name_list
if app.nn<len(name_list)-1:
app.nn=app.nn+1
app.text.destroy()
app.title.destroy()
showText()
else:
debug('这是最后一章啦,亲')
def tback():
global app
if app.nn>1:
app.nn=app.nn-1
app.title.destroy()
app.text.destroy()
showText()
else:
debug('这是第一章,亲')
def showText():
global app,url_list,name_list,con,url
app.clear()
app.title=Label(text=name_list[app.nn],\
font=("Microsoft YaHei",22,'bold'))
app.title.pack()
t=getText(url+'/'+url_list[app.nn])
app.text = Text(app.root, width=喵,height=16,\
exportselection=0,font=("Microsoft YaHei",18))
app.text.pack(side=TOP)
for x in t:
if not x=='':
x.replace(' ',' ')
ttt=x.find('<a href')
if not ttt==-1:
tttt=x.find('</div>')
x=x[:ttt]+x[tttt:]
try:
app.text.insert(INSERT,x+"\n")
except:
app.text.insert(INSERT,'?????\n')
c=len(name_list)-1
app.newButton(t='回主菜单',si=BOTTOM,c=mainManu)
app.newButton(t='换章',si=BOTTOM,c=\
lambda:znum(c))
app.newButton(t='下一章',si=BOTTOM,c=tnext)
app.newButton(t='上一章',si=BOTTOM,c=tback)
def save():
global app,url,url_list
t=getText(url+'/'+url_list[app.nn])
q=app.getEntry('lj')
f=open(q, 'a')
for x in t:
if len(x)>1:
f.write(x[24:]+'\n')
debug('保存完成啦,保存位置在'+q)
f.close()
znum(app.count)
def saveText():
global app
app.clear()
app.newText(t='输入保存路径', si=LEFT)
app.newButton(t='确定',si=RIGHT, c=save)
app.newEntry('lj', si=RIGHT)
def Link():
global app,url_list,name_list
app.clear()
app.newText(t='网址', si=TOP)
app.newEntry('url')
app.setEntry('url',url+'/'+url_list[app.nn])
app.newButton(t='回主菜单', c=mainManu)
def choose():
global app
n=app.getEntry('ZNum')
if not n=='':
try:
nn=int(n)
if nn>app.count:
debug('哎呀!!!数据太大了,范围是1~'\
+str(app.count))
app.clearEntry('ZNum')
raise IndexError(str(nn)+'out range')
except ValueError:
debug('小APP▄█▀█●跪了,{}不是数字'\
.format(n))
app.clearEntry('ZNum')
except IndexError:
pass
else:
app.nn=nn
app.clear()
app.newButton(t='在线阅读',si=LEFT,\
c=showText)
app.newButton(t='保存txt',si=LEFT,\
c=saveText)
app.newButton(t='链接',si=LEFT,\
c=Link)
def znum(a):
global app,name_list
app.clear()
try:
app.title.destroy()
app.text.destroy()
except:
pass
app.newText(t='章节号(1~'+str(a)+')',si=LEFT)
app.newEntry('ZNum',si=LEFT)
app.newButton(t='确定',c=choose, si=LEFT)
app.newButton(t='取消', c=mainManu, si=LEFT)
app.newButton(t='详细内容',\
c=lambda:debug('最后3章是:'+str(name_list[-4:\
-1])),si=RIGHT)
def getList():
global app,con,url_list,name_list
s2=con.find('<dl>')
e2=con.find('</dl>')
links=con[s2+4:e2]
links=links.split('<a href=\"')
for every_tag in links[1:]:
s=every_tag.find('>')
e=every_tag.find('</a>')
url_list.append(every_tag[0:s-1])
name_list.append(every_tag[s+1:e])
count=len(name_list)-1
app.count=count
znum(count)
def getTitle():
global app,t,url,url_list,name_list,con
nums=app.getEntry('Num')
if not nums=='':
try:
intnum=int(nums)
except:
debug('数据错误')
app.clearEntry('Num')
else:
url="https://www.77nt.com/"+str(intnum)
response = requests.get(url)
response.encoding = 'utf-8'
con=response.text
del(response)
s1=con.find('<h1>')
e1=con.find('</h1>')
title=con[s1+4:e1]
if not title.find('很抱歉,')==-1:
debug(t=\
'编号有误,亲,检查一下是不是输错了('\
+str(intnum)+')')
mainManu()
return 0
app.clear()
app.newText(t='你要找的小说是不是 《'\
+title+'》')
app.newButton(t='是',c=getList,si=LEFT)
app.newButton(t='不是', c=mainManu, \
si=RIGHT)
def clearE():
global app
app.clearEntry('Num')
def inputNum():
global app
app.clear()
app.newText(t='编号',si=LEFT)
app.newEntry('Num',si=LEFT)
app.newButton(t='清空',c=clearE,si=LEFT)
app.newButton(t='确定',c=getTitle,si=LEFT)
def mainManu():
global app,url_list,name_list
url_list=['#']
name_list=['#']
app.clear()
try:
app.text.destroy()
app.title.destroy()
except:
pass
app.newText(t="你通常怎样下载小说?",\
w=20, h=1, si=TOP)
app.newButton(t="退出", w=20,h=1,\
c=exit,si=BOTTOM)
app.newButton(t="编号", w=10,h=1,\
c=inputNum,si=LEFT)
app.newButton(t="小说名", w=10,h=1,\
c=debug,si=RIGHT)
mainManu()
app.start()