Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use tkinter to create a novel downloader in python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use tkinter in python to create a novel downloader. I hope you will get something after reading this article. Let's discuss it together.

Take a look at the effect picture first.

Tkinter is a module that uses python to design windows. The Tkinter module ("Tk interface") is the interface of Python's standard Tk GUI toolkit.

As a python-specific GUI interface, is an image window, tkinter is python's own, can be edited GUI interface, we can use GUI to achieve a lot of intuitive functions, such as want to develop a calculator, if it is only a program input, output window, it is useless user experience. It is necessary to develop a small graphic window.

Design a graphic interface first.

Code

From tkinter import * root = Tk () root.title ('novel downloader') root.geometry ('560x450 / 400 / 200') label = Label (root, text=', please enter the name of the downloaded novel:', font= ('Chinese script', 20) label.grid () entry = Entry (root, font= ('official script', 20) entry.grid (row=0, column=1) text= Listbox (root, font= ('official script', 16), width=50, heigh=15) text.grid (row=2, columnspan=2) button1 = Button (root, text=' start downloading' Font= ('official script', 15), command=search) button1.grid (row=3, column=0) button2 = Button (root, text=' exit program', font= ('official script', 15), command=root.quit) button2.grid (row=3, column=1) root.mainloop ()

Then, by parsing the data from the website, you can get the details page URL of the first novel.

The disadvantage of crawling static web pages is not great.

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 ()

Get the chapter, website and title of each novel

1. All chapter names and url addresses are included in the dd tag.

2. After obtaining the url, stitching is required.

/ 23Accord23019and11409705.html'# this is the url' http://www.xbiquge.la/23/23019/11409705.html' obtained from the web page # this is the url address of the real novel chapter content

3. The name of the novel can be obtained directly.

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 get_response sel.css'). Get () # extracted the download address of all chapters urls = sel.css ('# list dd a::attr (href)'). Getall () # not the latest 12 Chapter on the front main for url in urls: chapter_url = 'http://www.xbiquge.la' + url print (chapter_url)

Save and download each chapter of the novel

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 Grout 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 ()

Let's show the download again.

Def novel_load (title): text.insert (END, 'saving: {}' .format (title)) # text box scrolling text.see (END) # Update text.update () after reading this article, I believe you have some understanding of "how to use tkinter in python to create a novel downloader". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report