In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to write a weather query table software with GUI in Python". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to write a weather query table software with GUI in Python".
First, the realization effect second, basic train of thought
Import used libraries
Import requestsfrom lxml import etreeimport reimport tkinter as tkfrom PIL import Image, ImageTkfrom xpinyin import Pinyin1. Reptile part
Target url: https://lishi.tianqi.com/
The website provides historical weather forecasts for 2290 regions belonging to 34 provinces and cities across the country. The data come from the weather information of the city on the same day, and you can query historical weather conditions such as historical weather temperature, historical wind direction, historical wind and so on.
According to the analysis of the web page, it can be found that the url of all weather data for a region and a month is: https://lishi.tianqi.com/ + pinyin +'/'+ year and month .html. According to the region and time entered by the user, the string is processed, and the url is constructed, which is used for request to request the page with all the weather information of that month. After the response, the Xpath location extracts the weather information of the date to be queried by the user, and the query result is displayed in the tkinter interface.
The crawler code is as follows:
Def spider (): headers = {'user-agent':' Mozilla/5.0 (Windows NT 6.2) WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24' "referer": "https://lishi.tianqi.com/chengdu/index.html"} p = Pinyin () place = '.join (p.get_pinyin (b1.get ()) .split (' -')) # get the input of the regional text box into Pinyin # the time to process user input # stipulates that all three formats can be used on 2018-10-1 2018 2018-10-1 date = b2.get () # enter if'/'in date: tm_list = date.split ('/') elif'-'in date: tm_list = date.split ('-') else: tm_list = re.findall (r'\ dink') for the time text box Date) if int (tm_list [1]) < 10: # from January to September, add 0 tm_list [1] = frank0 {tm_list [1]}'# analyze the web page discovery rules and construct url # directly visit the page with all the weather information of that month to improve the query efficiency url = f "https://lishi.tianqi.com/{place}/{'' .join (tm_list [: 2])} .html "resp = requests.get (url) Headers=headers) html = etree.HTML (resp.text) # xpath location extraction of weather information for that day info = html.xpath (f'//ul [@ class= "thrui"] / li [{int (tm_list [2])}] / div/text ()') # output information format info1 = ['date:', 'maximum temperature:', 'minimum temperature:', 'Weather:' 'Wind direction:'] datas = [I + j for i, j in zip (info1, info)] info ='\ n'.join (datas) t.insert ('insert',' query results are as follows\ n\ n') t.insert ('insert', info) print (info) 2. Tkinter interface
The code is as follows:
Def get_image (file_nam, width, height): im = Image.open (file_nam). Resize ((width, height)) return ImageTk.PhotoImage (im) win = tk.Tk () # Settings window title and size win.title ('National Historical Weather query system') win.geometry ('500x500') # canvas settings background picture canvas = tk.Canvas (win, height=500, width=500) im_root = get_image (' test.jpg', width=500 Height=500) canvas.create_image (250,250,250, image=im_root) canvas.pack () # single-line text L1 = tk.Label (win, bg='yellow', text= "region:", font= ("SimHei", 12)) L2 = tk.Label (win, bg='yellow', text= "time:", font= ("SimHei", 12)) L1.place (xylene 85, yearly 100) L2.place (xylene 85, yearly 150) # single-line text box can collect keyboard input b1 = tk.Entry (win, font= ("SimHei") 12), show=None, width=35) b2 = tk.Entry (win, font= ("SimHei", 12), show=None, width=35) b1.place (xbug 140,100) b2.place (xbug 140,150) # set the query button and click to call the crawler function to implement the query a = tk.Button (win, bg='red', text= "query", width=25, height=2, command=spider) a.place (xbug 160) T.place 200) # set the color of the text when the font is selected in the multi-line text box width and height text box t = tk.Text (win, width=30, height=8, font= ("SimHei", 18), selectforeground='red') # display the multi-line text t.place (xtext 70, yearly 280) # enter the message cycle win.mainloop () Thank you for reading The above is the content of "how to write a weather query table software with GUI in Python". After the study of this article, I believe you have a deeper understanding of how to write a weather query table software with GUI in Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.