先看看运行截图
代码写入downs.py
"""
pip 下载源
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
豆瓣:http://pypi.douban.com/simple/
"""
import requests,os,re
from lxml.html import etree
source_dic={'1':'https://pypi.tuna.tsinghua.edu.cn/simple',
'2':'http://mirrors.aliyun.com/pypi/simple/',
'3':'http://pypi.douban.com/simple/'}
header={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.17 Safari/537.36'}
class Pip_downs:
def check_file_lines(self,filepath):#检查行数
count = 0
for index,line in enumerate(open(filepath, 'r')):
count += 1
print(index,line)
print('count',count)
def write_page(self,file_page,a):#写入文件
if not os.path.exists(file_page):
with open(file_page,'w',encoding='utf-8') as f:
f.write(a)
def read_page(self,file_page):#读取结果
with open(file_page,'r') as f:
res=f.read()
return res
def get_cons(self,flag):#先把html写入文件,输出文件列表
file_s=''#初始化字符串
url=''#初始化链接
if not os.path.exists('res/'):
os.mkdir('res/')
if flag=='1':
file_s='page_tsing.txt'
url=source_dic['1']
elif flag=='2':
file_s='page_ali.txt'
url=source_dic['2']
elif flag=='3':
file_s='page_douban.txt'
url=source_dic['3']
file_page='res/'+file_s
res=requests.get(url,headers=header).text
self.write_page(file_page,res)#写入文件
res=self.read_page(file_page)#读取文件
html=etree.HTML(res)
lis=html.xpath('/html/body/a/text()')
print('len_page',len(lis))
return lis
def get_files_url(self,flag,name):
base_url=''
if flag=='1':
base_url=source_dic['1']
elif flag=='2':
base_url=source_dic['2']
elif flag=='3':
base_url=source_dic['3']
full_url=base_url+'/'+name
new_res=requests.get(full_url).content.decode('utf-8')
# print(new_res)
html=etree.HTML(new_res)
file_names=html.xpath('//body/a/text()')
file_urls=html.xpath('//body/a/@href')
print(len(file_names))
files_dic={}
for name,url in zip(file_names,file_urls):
files_dic[name]=base_url[:-7]+url[5:]
print(files_dic)
return files_dic
def down_file(self,flag,name,name_s,path):
files_dic=self.get_files_url(flag,name)
url=files_dic[name_s]
fi=requests.get(url).content
with open(path+'/'+name_s,'wb') as f:
f.write(fi)
print('{}已下载完成'.format(name))
tkinter代码如下
from tkinter.ttk import Button,Entry,Radiobutton
from tkinter import Tk,Label,StringVar,Scrollbar,END,messagebox,Listbox,VERTICAL,HORIZONTAL,SINGLE
from tkinter.filedialog import askdirectory
from python库下载器.downs import Pip_downs
import os
class App:
def __init__(self):
self.w=Tk()
self.w.geometry('320x340')
self.w.title('python库下载器V2.1 中秋版')
self.creat_res()
self.res_config()#配置文件
self.pp=Pip_downs()
self.w.mainloop()
def creat_res(self):#创建控件
self.temp=StringVar()#输入框
self.path_file=StringVar()#存储路径
self.sorce=StringVar()#选择源
self.B_search=Button(self.w,text='查找')
self.B_file=Button(self.w,text='选择存储目录')
self.B_down=Button(self.w,text='下载')
self.B_add=Button(self.w,text='添加')
self.L_search=Label(self.w,text='选择源:',bg='#B2DFEE')
self.E_input=Entry(self.w,textvariable=self.temp)
self.E_path=Entry(self.w,textvariable=self.path_file)
self.L_pack=Label(self.w,text='找到:',bg='#B2DFEE')
self.L_packs=Label(self.w,text='找到:',bg='#B2DFEE')
self.L_box=Listbox(self.w,selectmode=SINGLE)
self.L_box_se=Listbox(self.w,selectmode=SINGLE)
self.S_col_y_1=Scrollbar(self.w,orient=VERTICAL)#左边Y
self.S_col_x_1=Scrollbar(self.w,orient=HORIZONTAL)#左边X
self.S_col_y_2=Scrollbar(self.w,orient=VERTICAL)#右边Y
self.S_col_x_2=Scrollbar(self.w,orient=HORIZONTAL)#右边X
self.L_message=Label(self.w,text='',bg='#C6E2FF')
self.creat_radios()
self.res_place()
def creat_radios(self):#选择器
self.R_qinghua=Radiobutton(self.w,text='清华',variable=self.sorce,value='1')
self.R_alibaba=Radiobutton(self.w,text='阿里',variable=self.sorce,value='2')
self.R_douban=Radiobutton(self.w,text='豆瓣',variable=self.sorce,value='3')
def res_place(self):#控件布局
self.L_search.place(x=10,y=10,width=100,height=20)
self.B_search.place(x=200,y=40,width=50,height=30)
self.E_input.place(x=10,y=40,width=170,height=30)
self.E_path.place(x=10,y=80,width=170,height=30)
self.L_pack.place(x=10,y=113,width=200,height=23)
self.L_packs.place(x=220,y=113,width=90,height=23)
self.B_file.place(x=200,y=80,width=90,height=30)
self.R_qinghua.place(x=110,y=10,width=60,height=20)
self.R_alibaba.place(x=170,y=10,width=60,height=20)
self.R_douban.place(x=230,y=10,width=60,height=20)
self.L_box.place(x=10,y=140,width=140,height=175)
self.L_box_se.place(x=180,y=140,width=120,height=120)
self.B_down.place(x=250,y=279,width=50,height=28)
self.B_add.place(x=186,y=279,width=50,height=28)
self.S_col_y_1.place(x=150,y=140,width=15,height=175)
self.S_col_x_1.place(x=10,y=315,width=140,height=15)
self.S_col_y_2.place(x=300,y=140,width=15,height=120)
self.S_col_x_2.place(x=180,y=260,width=120,height=15)
self.L_message.place(x=170,y=308,width=140,height=28)
def res_config(self):#滚动条配置
self.S_col_y_1.config(command=self.L_box.yview)
self.L_box["yscrollcommand"] = self.S_col_y_1.set
self.S_col_x_1.config(command=self.L_box.xview)
self.L_box["xscrollcommand"] = self.S_col_x_1.set
self.S_col_y_2.config(command=self.L_box_se.yview)
self.L_box_se["yscrollcommand"] = self.S_col_y_2.set
self.S_col_x_2.config(command=self.L_box_se.xview)
self.L_box_se["xscrollcommand"] = self.S_col_x_2.set
self.B_file.config(command=self.select_menu)
self.B_search.config(command=self.search_res)
self.sorce.set('1')
self.B_add.config(command=self.add_pack_to_box)
self.B_down.config(command=self.lis_box_down)
def select_menu(self):
self.path_f=askdirectory()
self.path_file.set(self.path_f)
def clear_box(self):
self.L_box.delete(0,END)
def search_res(self):#查找库
print('search')
print(self.temp.get())
self.clear_box()
lis_new=[]
if self.temp.get()!='':
lis=self.pp.get_cons(self.sorce.get())
count=0
for s_str in lis:
if self.temp.get() in s_str:
print('找到库{}'.format(s_str))
lis_new.append(s_str)
else:
count+=1
if count==len(lis):
messagebox.showwarning(title='警告',message='没找到库{}'.format(self.temp.get()))
msg='找到{}个与{}相关的库'.format(len(lis_new),self.temp.get())
print(msg)
self.L_pack.config(text=msg)
for msg in lis_new:
self.L_box.insert(END,msg)
return lis_new
else:
messagebox.showerror(title='警告',message='请输入库名')
def add_pack_to_box(self):#添加列表至lisbox
cur_index=self.L_box.curselection()
print(cur_index)
if len(cur_index)!=0:
files_dic=self.pp.get_files_url(self.sorce.get(),self.L_box.get(cur_index))
self.L_packs.config(text='各版本{}个'.format(len(files_dic)))
for name,url in files_dic.items():
self.L_box_se.insert(END,name)
else:
messagebox.showwarning(title='警告',message='请选择库')
def lis_box_down(self):
print('down')
cur_index=self.L_box_se.curselection()
if len(cur_index)!=0:
print('路径',self.path_file.get())
if self.path_file.get()!='':
pack_name=self.L_box_se.get(cur_index).split('-')[0]
if '_' in pack_name:
pack_name=pack_name.replace('_','-')
print(self.sorce.get(),pack_name,self.L_box_se.get(cur_index),self.path_file.get())
self.pp.down_file(self.sorce.get(),pack_name,self.L_box_se.get(cur_index),self.path_file.get())
self.L_message.config(text='{}已下载'.format(pack_name))
else:
messagebox.showwarning(title='警告',message='请选择存储路径')
else:#如果没选择
messagebox.showwarning(title='警告',message='请选择库')
a=App()