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 save high-definition pictures with Python+tkinter

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article "how to use Python+tkinter to achieve high-definition picture preservation" most people do not understand, so the editor summed up the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "how to use Python+tkinter to achieve high-definition picture preservation" article.

Basic development environment

Pycharm

Python 3.8

Main related modules

Request,BeautifulSoup,tkinter (Python built-in library, just import it directly)

Let's take a look at the final effect (Python+tkinter (graphical Interface Design)). The high-definition image is saved to the local computer.

(now you just need to type your own py file name of pyinstaller-F-w. Py in the console. Py) and you can pack it into exe and put it on the desktop.

Analyze web pages

Before crawling, the first step is to analyze the web page to determine whether the web page is static or dynamic. It is easy to start by knowing yourself and the enemy, right? In order to avoid the difficulty of climbing and save time.

We right-click on the web page to check the input keywords to find the information about the picture, and we can roughly confirm that the site is static. Then we can crawl the web page according to the common method.

Get to work.

1.1

First construct a forged header to prevent simple anti-crawling, then make a request to the web page, and if the status code of the object I requested is 200 (successful access), then return the text text to me.

1.2

After the web request is successful, we can analyze the location of the image on the web page.

Customs observation found that the pictures we want are stored in the ul tag under the tag div class='list', and the li under the ul tag are all the pictures we need. We found the location, so then instantiate a soup object to find all the li tags, then loop through each li tag, get the tag b inside the li tag to get the name of the picture, and then go to the img tag src attribute to extract the image link.

The following code demonstrates.

1.3

The name of the picture and the link to the picture are available, and the next step is to create a storage location. Because the picture is binary data, it is requested as content, and finally written to the folder as wb.

Analysis of the page, it is not difficult to find the law of page turning, as long as the cycle can be achieved, page turning to achieve page crawling. (if you want more pages, change the numbers yourself.)

Index_2.htm

Index_3.htm

Index_4.htm

Finally, set up the tkinter graphical interface (no more details here, you can check it on the blog), the combination of design and program will be completed.

Realize

All code # @ Author: import requestsfrom bs4 import BeautifulSoupimport osimport tkinter as tk # GUIimport concurrent.futuresimport threadingdef get_content (url): headers = {'user-agent':' Mozilla/5.0 (Windows NT 6.1; Win64) X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'} response = requests.get (url,headers) response.encoding = response.apparent_encoding # automatic transcoding if response.status_code = = 200: return response.textdef get_data (response): soup = BeautifulSoup (response 'lxml') all_li = soup.find (class_= "list"). Find (' ul') for i in all_li.find_all ('li'): if i.find (' b') is not None: title = i.find ('b'). Text else: title = 'NOT' images = i.find (' a'). Find ('img'). Get ('src') save_images (title Images) def save_csv (): passdef save_images (title,images): if not os.path.exists ('img'): # create a folder os.mkdir (' img') headers = {'user-agent':' Mozilla/5.0 (Windows NT 6.1) Win64 X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'} images_data = requests.get (url=images,headers=headers). Content with open ('img\\' + title + '.jpg', mode='wb') as f: f.write (images_data) print ('saving = >:, title) # GUI text box enter qq.insert (tk.INSERT "saving pictures:" + title +'\ n') qq.yview_moveto (1) qq.update () def main (): print ('= button has been clicked = =') for i in range (2Power11): url = f 'http://www.netbian.com/index_{i}.htm' # Loop qq.insert (tk.INSERT Response = saving the picture on page {I} = ='+'\ n') qq.update () print (favored = saving the data content on page {I} =) response = get_content (url) get_data (response) qq.insert (tk.INSERT) '= = end of save =') # Multithreading prevents Gui from getting stuck def process_it (): it = threading.Thread (target=main) it.setDaemon (True) it.start () if _ _ name__ = ='_ _ main__': # set GUI graphical interface windoms = tk.Tk () windoms.iconbitmap () windoms.title ('picture') windoms.geometry ('500x500' 650') # labal text = tk.Label (windoms Text=' Picture Mini Program', font= ('Chinese New Wei', 20)) text.place (Xerox 170, font= (20), width=30,height=4,fg='Violet',bd=8,command=process_it) # Button button = tk.Button (windoms,text=' starts downloading', font= (20), width=30,height=4,fg='Violet',bd=8,command=process_it) # start downloading button.place (Xue140 Magazine 80) button1 = tk.Button (windoms,text=' exit', font= (20), width=30,fg='Violet',height=4,bd=8) Command=windoms.quit) # exit button1.place (Xbox 140, yearly 180) # text box qq = tk.Text (windoms,state='normal',bg='light cyan',fg='DeepPink') qq.place (rely=0.6,relheight=0.4) # display window tk.mainloop () above is the content of this article on "how to use Python+tkinter to save high-definition pictures" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

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

Development

Wechat

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

12
Report