python中如何使用tkinter打造一個(gè)小說(shuō)下載器

小編給大家分享一下python中如何使用tkinter打造一個(gè)小說(shuō)下載器,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

站在用戶(hù)的角度思考問(wèn)題,與客戶(hù)深入溝通,找到九臺(tái)網(wǎng)站設(shè)計(jì)與九臺(tái)網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶(hù)體驗(yàn)好的作品,建站類(lèi)型包括:網(wǎng)站建設(shè)、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊(cè)、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋九臺(tái)地區(qū)。

先看下效果圖

python中如何使用tkinter打造一個(gè)小說(shuō)下載器

Tkinter 是使用 python 進(jìn)行窗口視窗設(shè)計(jì)的模塊。Tkinter模塊("Tk 接口")是Python的標(biāo)準(zhǔn)Tk GUI工具包的接口。

作為 python 特定的GUI界面,是一個(gè)圖像的窗口,tkinter是python 自帶的,可以編輯的GUI界面,我們可以用GUI 實(shí)現(xiàn)很多直觀的功能,比如想開(kāi)發(fā)一個(gè)計(jì)算器,如果只是一個(gè)程序輸入,輸出窗口的話,是沒(méi)用用戶(hù)體驗(yàn)的。所有開(kāi)發(fā)一個(gè)圖像化的小窗口,就是必要的。

python中如何使用tkinter打造一個(gè)小說(shuō)下載器

先設(shè)計(jì)一個(gè)圖像化的界面

代碼

from tkinter import *

root = Tk()
root.title('小說(shuō)下載器')
root.geometry('560x450+400+200')

label = Label(root, text='請(qǐng)輸入下載小說(shuō)名字:', font=('華文行楷', 20))
label.grid()

entry = Entry(root, font=('隸書(shū)', 20))
entry.grid(row=0, column=1)

text = Listbox(root, font=('隸書(shū)', 16), width=50, heigh=15)
text.grid(row=2, columnspan=2)

button1 = Button(root, text='開(kāi)始下載', font=('隸書(shū)', 15), command=search)
button1.grid(row=3, column=0)

button2 = Button(root, text='退出程序', font=('隸書(shū)', 15), command=root.quit)
button2.grid(row=3, column=1)

root.mainloop()

python中如何使用tkinter打造一個(gè)小說(shuō)下載器

然后通過(guò)解析網(wǎng)站數(shù)據(jù),獲取第一個(gè)小說(shuō)的詳情頁(yè)URL即可。

靜態(tài)網(wǎng)頁(yè)的爬取,缺點(diǎn)是不大的。

def search():
    search_url = 'http://www.xbiquge.la/modules/article/waps.php'
    data = {
        'searchkey': name
    }
    response = requests.post(url=search_url, data=data, headers=headers)
    selector = get_parsing(response.text)
    novel_url = selector.css('.even a::attr(href)').extract_first()

獲取每本小說(shuō)的章節(jié)網(wǎng)址以及小說(shuō)名字

1,所有的章節(jié)名稱(chēng)以及url地址都包含在dd標(biāo)簽里面。

python中如何使用tkinter打造一個(gè)小說(shuō)下載器

2,獲取url后,需要拼接

'/23/23019/11409705.html' # 這是網(wǎng)頁(yè)獲取到的url
'http://www.xbiquge.la/23/23019/11409705.html' # 這是真實(shí)的小說(shuō)章節(jié)內(nèi)容url地址

3,小說(shuō)名字,直接獲取即可。

def download_one_book(index_url):
    response = get_response(index_url)
    response.encoding = response.apparent_encoding
    sel = get_parsing(response.text)
    book_name = sel.css('#info h2::text').get()
    # 提取了所有章節(jié)的下載地址
    urls = sel.css('#list dd a::attr(href)').getall()
    # 不要最新的 12 章放在最前main
    for url in urls:
        chapter_url = 'http://www.xbiquge.la' + url
        print(chapter_url)

保存下載每章小說(shuō)內(nèi)容

def download_one_chapter(chapter_url, book_name):
    response = get_response(chapter_url)
    response.encoding = response.apparent_encoding
    html = response.text
    selector = get_parsing(html)
    h2 = selector.css('.bookname h2::text').get()
    content = selector.css('#content::text').getall()
    lines = []

    for c in content:
        lines.append(c.strip())
    print(h2)
    text = '\n'.join(lines)
    file = open(book_name + '.txt', mode='a', encoding='utf-8')
    file.write(h2)
    file.write('\n')
    file.write(text)
    file.write('\n')
    file.close()

再來(lái)個(gè)顯示下載內(nèi)容

def novel_load(title):
    text.insert(END, '正在保存:{}'.format(title))
    # 文本框滾動(dòng)
    text.see(END)
    # 更新
    text.update()

看完了這篇文章,相信你對(duì)“python中如何使用tkinter打造一個(gè)小說(shuō)下載器”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)頁(yè)名稱(chēng):python中如何使用tkinter打造一個(gè)小說(shuō)下載器
當(dāng)前地址:http://bm7419.com/article48/jdssep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站營(yíng)銷(xiāo)電子商務(wù)、品牌網(wǎng)站制作定制開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化