用python简易制作晋江城小说下载器(GUI+爬虫+多线程)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/jjsjsjjdj/article/details/102750655
import requests
from bs4 import BeautifulSoup
import re
import os
import pandas as pd



#0.获取网页基本信息
def get_html(url):
    headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; rv:70.0)"+"Gecko/20100101 Firefox/70.0"} #设置请求头
    r=requests.get(url,headers=headers)  
    html=r.text.encode(r.encoding).decode("GBK")      
    soup=BeautifulSoup(html,"lxml") 
    return soup

# 1.获取小说名称
def get_article_title(main_url):
    soup=get_html(main_url)
    article_title=soup.find("span",itemprop="articleSection").text
    return article_title


# 2.获取所有章节的地址
def get_allurl(url):
    soup=get_html(url)
    href=soup.findAll("a",itemprop="url") 
    allurl=[i.attrs["href"]  for i in href]
    return allurl

# 3.小说章节下载
def chapter_Download(file_savePath,url,article_title,number):
    
    #1.获取网页数据
    soup=get_html(url)
    
    #2.清洗数据
    title=soup.find("div",align="center").h2.text
   
    content=soup.find("div",class_="noveltext").text
    content=re.sub("(\r|\n|\u3000|\xa0)","",content)  #出去换行标记等等
    content=re.sub("插入书签","",content)
    content=re.sub("电子书下载TXT下载举报色情反动举报刷分其他文章收藏 为收藏文章分类定制收藏类别查看收藏列表","",content)
    content=re.sub("\[.*?\]","",content)  #懒惰匹配 *? : 1.找最近的()匹配  
    content=re.sub("\(.*\)","",content)  #懒惰匹配 *? : 1.找最近的()匹配  
    content=re.sub(title,"",content)
    content=re.sub("                            ","",content)
    content=re.sub("displayyrt","",content)
    content=re.sub(";"," ",content)
    
    #3.保存小说
    filedir=file_savePath+"/《"+article_title+"》"    #1.创建路径
    if not os.path.exists(filedir):    #2.创建目录
        os.mkdir(filedir) 
        
    with open(filedir+"/"+str(number)+".%s.txt"%title,mode="w",encoding="utf-8") as f:     #打开文件,放入内容
         f.write(title+"\n"+content) 
         print(filedir+title+"下载中")

            
#4.小说下载            
def novel_Download(index):            
    try:
        #1.获取小说主页的地址
        index=int(index)
        base="http://www.jjwxc.net/onebook.php?novelid=" 
        main_url=base+str(index) 
        
        
        #2.下载预处理处理工作
        file_savePath="E:/小说"      #1.存放路径    
        allurl=get_allurl(main_url)       #2.获取所有章节地址的集合
        article_title=get_article_title(main_url) #3.获取小说名称
        number=1           #4.章节编号
                 
        #3.遍历下载每一章节
        for url in allurl:
            t=threading.Thread(target=chapter_Download(file_savePath,url,article_title,number),args=())
            t.start()
            number+=1
      

        print("下载完成")
        print("文件存放在",file_savePath)
        return True
    except:
        print("下载失败")
        return False
    finally:
        print("谢谢使用")
        


#---------------------------------------------------------------------------------------------


import tkinter as tk
import tkinter.messagebox
from tkinter import *
import tkinter.filedialog
import threading
import tkinter.scrolledtext

filename=""

#----------------------一、定义按钮功能--------------------------------------
def fun1():
    x=en1.get()   #1.获取文本
    p=novel_Download(x)
    if p==True: #2.验证
        tk.messagebox.showinfo(message="下载完成")#弹出提示框
    else:
        tk.messagebox.showerror(message="下载失败")#弹出错误框
  
def fun2():
    global filename
    filename=tk.filedialog.askopenfilename(title="openfile",initialdir="E:/小说")#访问文件夹
    pass

    
 
#0.创建界面
root=tk.Tk()         #1.创建界面
root["height"]=150   #2.定义界面高度
root["width"]=300    #3.定义界面宽度




#1.文本框
lal=tk.Label(root,text="欢迎使用小说下载器")      #在root中创建标签
lal.place(x=50,y=15,width=200,height=50)  #向root放置标签

#2.输入框
lal=tk.Label(root,text="请输入书的id")       #在root中创建标签
lal.place(x=20,y=55,width=80,height=20)  #向root放置标签
en1=tk.Entry(root)                       #在root中创建文本框
en1.place(x=100,y=55,width=165,height=20) #向root放置文本框

#3.下载按钮
b1=tk.Button(root,text="下载",command=fun1)  #定义:按钮名称+按钮功能
b1.place(x=50,y=90,width=100)          #定义:按钮大小+按钮位置

#4.查看按钮
b2=tk.Button(root,text="查看",command=fun2)
b2.place(x=180,y=90,width=100)


root.mainloop()#界面生成

猜你喜欢

转载自blog.csdn.net/jjsjsjjdj/article/details/102750655