所以我有这段代码:
from urllib2 import *
import re
import tkSimpleDialog
import os
import glob
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
image1 = PhotoImage(file="picture.gif")
w = image1.width()
h = image1.height()
master.geometry("%dx%d+0+0" % (w, h))
# tk.Frame has no image argument
panel1 = Label(master, image=image1)
panel1.pack(side='top', fill='both', expand='yes')
panel1.image = image1
self.e = Entry(frame)
self.e.grid(row=0, column=0)
b = Button(frame, text='Search', command=self.whale)
b.grid(row=0, column=1)
###
def whale(self):
page = '1'
def newre():
page = '1'
pages = int(page)
peg = pages + 1
pege = str(peg)
page = pege
self.whale()
alist = []
mu = self.e.get()
print mu
mus = mu.replace(' ','-')
op = urlopen('http://website' + mus + '-'+page+'.html')
ops = op.read()
if 'charset="utf-8">var playlist' in ops:
print 'yes'
cal = ops.split('charset="utf-8">var playlist',1)
del cal[0]
cv = str(cal)
cals = cv.split('},];</script><div',1)
del cals[1]
cals = str(cals)
v = cals.replace('{', '''
''')
vn = v.replace('[','')
vnm = vn.replace(']','')
parsed = re.findall('author : "([^"]+)",title : "([^"]+)",type : "([^"]+)",file : "([^"]+)",id : "([^"]+)', vnm)
print mus
cvb0 = ','.join(map(''.join,parsed[0]))
cvb1 = ','.join(map(''.join,parsed[1]))
cvb2 = ','.join(map(''.join,parsed[2]))
cvb3 = ','.join(map(''.join,parsed[3]))
cvb4 = ','.join(map(''.join,parsed[4]))
cvb5 = ','.join(map(''.join,parsed[5]))
cvb6 = ','.join(map(''.join,parsed[6]))
cvb7 = ','.join(map(''.join,parsed[7]))
cvb8 = ','.join(map(''.join,parsed[8]))
cvb9 = ','.join(map(''.join,parsed[9]))
canada0 = cvb0.split('sound,',1)
canada1 = cvb1.split('sound,',1)
canada2 = cvb2.split('sound,',1)
canada3 = cvb3.split('sound,',1)
canada4 = cvb4.split('sound,',1)
canada5 = cvb5.split('sound,',1)
canada6 = cvb6.split('sound,',1)
canada7 = cvb7.split('sound,',1)
canada8 = cvb8.split('sound,',1)
canada9 = cvb9.split('sound,',1)
song0 = canada0[0]
song1 = canada1[0]
song2 = canada2[0]
song3 = canada3[0]
song4 = canada4[0]
song5 = canada5[0]
song6 = canada6[0]
song7 = canada7[0]
song8 = canada8[0]
song9 = canada9[0]
del canada0[0]
del canada1[0]
del canada2[0]
del canada3[0]
del canada4[0]
del canada5[0]
del canada6[0]
del canada7[0]
del canada8[0]
del canada9[0]
run0 = str(canada0)
run1 = str(canada1)
run2 = str(canada2)
run3 = str(canada3)
run4 = str(canada4)
run5 = str(canada5)
run6 = str(canada6)
run7 = str(canada7)
run8 = str(canada8)
run9 = str(canada9)
#run0
runs0 = str(run0)
nun0 = runs0.split(',',1)
del nun0[1]
nun0 = str(nun0)
run0 = nun0.replace('[','')
runs0 = run0.replace(']','')
runss0 = runs0.replace('"','')
runsss0 = runss0.replace("'",'')
#run1
runs1 = str(run1)
nun1 = runs1.split(',',1)
del nun1[1]
nun1 = str(nun1)
run1 = nun1.replace('[','')
runs1 = run1.replace(']','')
runss1 = runs1.replace('"','')
runsss1 = runss1.replace("'",'')
#run2
runs2 = str(run2)
nun2 = runs2.split(',',1)
del nun2[1]
nun2 = str(nun2)
run2 = nun2.replace('[','')
runs2 = run2.replace(']','')
runss2 = runs2.replace('"','')
runsss2 = runss2.replace("'",'')
#run3
runs3 = str(run3)
nun3 = runs3.split(',',1)
del nun3[1]
nun3 = str(nun3)
run3 = nun3.replace('[','')
runs3 = run3.replace(']','')
runss3 = runs3.replace('"','')
runsss3 = runss3.replace("'",'')
#run4
runs4 = str(run4)
nun4 = runs4.split(',',1)
del nun4[1]
nun4 = str(nun4)
run4 = nun4.replace('[','')
runs4 = run4.replace(']','')
runss4 = runs4.replace('"','')
runsss4 = runss4.replace("'",'')
#run5
runs5 = str(run5)
nun5 = runs5.split(',',1)
del nun5[1]
nun5 = str(nun5)
run5 = nun5.replace('[','')
runs5 = run5.replace(']','')
runss5 = runs5.replace('"','')
runsss5 = runss5.replace("'",'')
#run6
runs6 = str(run6)
nun6 = runs6.split(',',1)
del nun6[1]
nun6 = str(nun6)
run6 = nun6.replace('[','')
runs6 = run6.replace(']','')
runss6 = runs6.replace('"','')
runsss6 = runss6.replace("'",'')
#run7
runs7 = str(run7)
nun7 = runs7.split(',',1)
del nun7[1]
nun7 = str(nun7)
run7 = nun7.replace('[','')
runs7 = run7.replace(']','')
runss7 = runs7.replace('"','')
runsss7 = runss7.replace("'",'')
#run8
runs8 = str(run8)
nun8 = runs8.split(',',1)
del nun8[1]
nun8 = str(nun8)
run8 = nun8.replace('[','')
runs8 = run8.replace(']','')
runss8 = runs8.replace('"','')
runsss8 = runss8.replace("'",'')
#run9
runs9 = str(run9)
nun9 = runs9.split(',',1)
del nun9[1]
nun9 = str(nun9)
run9 = nun9.replace('[','')
runs9 = run9.replace(']','')
runss9 = runs9.replace('"','')
runsss9 = runss9.replace("'",'')
def download0():
print 'downloading...'
url = runsss0
down = urlopen(url)
filepath = 'C:\Users\Psao\Desktop\whalewhire\Downloaded\ ' + song0 + '.mp3'
local = open(filepath, "wb")
local.write(down.read())
print 'done'
def download1():
print 'downloading...'
url = runsss1
down = urlopen(url)
filepath = 'C:\Users\Psao\Desktop\whalewhire\Downloaded\ ' + song1 + '.mp3'
local = open(filepath, "wb")
local.write(down.read())
print 'done'
def download2():
print 'downloading...'
url = runsss2
down = urlopen(url)
filepath = 'C:\Users\Psao\Desktop\whalewhire\Downloaded\ ' + song2 + '.mp3'
local = open(filepath, "wb")
local.write(down.read())
print 'done'
def download3():
print 'downloading...'
url = runsss3
down = urlopen(url)
filepath = 'C:\Users\Psao\Desktop\whalewhire\Downloaded\ ' + song3 + '.mp3'
local = open(filepath, "wb")
local.write(down.read())
print 'done'
def download4():
print 'downloading...'
url = runsss4
down = urlopen(url)
filepath = 'C:\Users\Psao\Desktop\whalewhire\Downloaded\ ' + song4 + '.mp3'
local = open(filepath, "wb")
local.write(down.read())
print 'done'
def download5():
print 'downloading...'
url = runsss5
down = urlopen(url)
filepath = 'C:\Users\Psao\Desktop\whalewhire\Downloaded\ ' + song5 + '.mp3'
local = open(filepath, "wb")
local.write(down.read())
print 'done'
def download6():
print 'downloading...'
url = runsss6
down = urlopen(url)
filepath = 'C:\Users\Psao\Desktop\whalewhire\Downloaded\ ' + song6 + '.mp3'
local = open(filepath, "wb")
local.write(down.read())
print 'done'
def download7():
print 'downloading...'
url = runsss7
down = urlopen(url)
filepath = 'C:\Users\Psao\Desktop\whalewhire\Downloaded\ ' + song7 + '.mp3'
local = open(filepath, "wb")
local.write(down.read())
print 'done'
def download8():
print 'downloading...'
url = runsss8
down = urlopen(url)
filepath = 'C:\Users\Psao\Desktop\whalewhire\Downloaded\ ' + song8 + '.mp3'
local = open(filepath, "wb")
local.write(down.read())
print 'done'
def download9():
print 'downloading...'
url = runsss9
down = urlopen(url)
filepath = 'C:\Users\Psao\Desktop\whalewhire\Downloaded\ ' + song9 + '.mp3'
local = open(filepath, "wb")
local.write(down.read())
print 'done'
def replay():
Admin.destroy()
os.system('WhaleWire.py')
def library():
path = 'C:\Users\Psao\Desktop\whalewhire\Downloaded'
aw=[]
for infile in glob.glob( os.path.join(path,'*.mp3') ):
libr = infile.split('Downloaded',1)
aw.append('\n')
aw.append(infile)
la = Label(Admin,width=100,height=50, text=aw).grid(row=0,column=7)
b2s = Button(Admin,text='Search', command=replay).grid(row=0,column=8)
print song1
Label(Admin, text=song0).grid(row=1)
Label(Admin, text=song1).grid(row=2)
Label(Admin, text=song2).grid(row=3)
Label(Admin, text=song3).grid(row=4)
Label(Admin, text=song4).grid(row=5)
Label(Admin, text=song5).grid(row=6)
Label(Admin, text=song6).grid(row=7)
Label(Admin, text=song7).grid(row=8)
Label(Admin, text=song8).grid(row=9)
Label(Admin, text=song9).grid(row=10)
b0 = Button(Admin, text='Download', command=download0)
b1 = Button(Admin, text='Download', command=download1)
b2 = Button(Admin, text='Download', command=download2)
b3 = Button(Admin, text='Download', command=download3)
b4 = Button(Admin, text='Download', command=download4)
b5 = Button(Admin, text='Download', command=download5)
b6 = Button(Admin, text='Download', command=download6)
b7 = Button(Admin, text='Download', command=download7)
b8 = Button(Admin, text='Download', command=download8)
b9 = Button(Admin, text='Download', command=download9)
b10 = Button(Admin, text='More results', command=newre)
b11 = Button(Admin, text='Library', command=library)
b0.grid(row=1, column=1)
b1.grid(row=2, column=1)
b2.grid(row=3, column=1)
b3.grid(row=4, column=1)
b4.grid(row=5, column=1)
b5.grid(row=6, column=1)
b6.grid(row=7, column=1)
b7.grid(row=8, column=1)
b8.grid(row=9, column=1)
b9.grid(row=10, column=1)
b10.grid(row=10, column=2)
b11.grid(row=11, column=3)
Admin = Tk()
Admin.title('WhaleWires')
app = App(Admin)
Admin.mainloop()
我知道这很长很重复,但我不想花时间为每件事找到干净的代码。
当我用两个单词输入一首歌时,它会给我一个不同的错误
an Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "C:\Users\Psao\Desktop\whalewhire\WhaleWire.py", line 93, in whale
cvb7 = ','.join(map(''.join,parsed[7]))
IndexError: list index out of range
但解析[#]有点不同
任何帮助,如果您知道任何会缩短我的代码的代码,我会很高兴。
答案 0 :(得分:1)
似乎parsed
有时短于10件
0-9变量应该在列表而不是单个变量中,这将允许人们使用循环对值重复相同的操作,而不是复制每个变量的代码