校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > Python實現天氣查詢軟件

Python實現天氣查詢軟件

熱門標簽:騰訊地圖標注沒法顯示 商家地圖標注海報 地圖標注自己和別人標注區別 ai電銷機器人的優勢 海外網吧地圖標注注冊 孝感營銷電話機器人效果怎么樣 打電話機器人營銷 聊城語音外呼系統 南陽打電話機器人

一、背景

某天下班淋雨成了落湯雞,發了個朋友圈感慨一下啊,然后......

夜深人靜之時,突然收到了來自學妹的Py文件,運行之后發現事情并不簡單(如下圖):

這是暗示我...下次出門給她帶把傘?不管那么多,作為一個程序猿,遇到程序先拆解一下。

二、工具

爬蟲:requests

解析:re

UI:tkinter

三、代碼解讀

想要做一個獲取天氣預報的小程序,第一步要做的就是能夠進行天氣預報的爬取,這里通過城市名稱匹配百度天氣的URL進行爬取,并通過正則的方式進行解析,最終以字典的形式返回結果。

class Weather(object):
    def __init__(self):
        pass
 
    def crawl(self, key):
 
        url = 'http://weathernew.pae.baidu.com/weathernew/pc?query=' + key + '天氣srcid=4982city_name=' + key + 'province_name=' + key
 
        # 設置請求頭
        headers = {
            'user-agent':
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
            'Referer': 'https://googleads.g.doubleclick.net/'
        }
        # 頁面HTML
        res = requests.get(url, headers=headers).text
        # 時間
        time = re.search(r'\{\"update_time\":\"(.+?)\"', res).group(1)
        # 天氣
        weather = re.search(r'\"weather\"\:\"(.+?)\"', res).group(1)
        weather = weather.encode('utf-8').decode("unicode-escape")
        # 氣溫
        weather_l = re.search(r'temperature_night\"\:\"(.+?)\"', res).group(1)
        weather_h = re.search(r'temperature_day\"\:\"(.+?)\"', res).group(1)
        # 風力
        wind_now = re.search(r'\"wind_power_day\"\:\"(.+?)\"', res).group(1)
        wind_now = wind_now.encode('utf-8').decode("unicode-escape")
        wind_name = re.search(r'\"wind_direction_day\"\:\"(.+?)\"',
                              res).group(1)
        wind_name = wind_name.encode('utf-8').decode("unicode-escape")
        wind = wind_name + wind_now
        # 貼示
        desc = re.search(r'\"desc\"\:\"(.+?)\"', res).group(1)
        desc = desc.encode('utf-8').decode("unicode-escape")
 
        # 結果生
        dic = {
            '城市': key,
            '更新時間': time,
            '天氣': weather,
            '溫度': weather_l + '-' + weather_h + '攝氏度',
            '風力': wind,
            '貼示': desc,
        }
        return dic

寫好了爬取天氣預報的代碼之后,下面就可以寫一個UI來和輸入/爬取的內容進行交互的,寫UI的方式大同小異,代碼如下:

class Weather_UI(object):
    def __init__(self):
        self.window = Tk()
        self.weather = Weather()
 
        self.window.title(u'天氣預報')
        # 設置窗口大小和位置
        self.window.geometry('310x370')
        # 創建一個文本框
        self.result_text0 = Label(self.window, text=u'學長所在城市:\n要寫中文呦')
        self.result_text0.place(x=10, y=5, height=130)
        self.result_text0.bind('提示')
 
        self.result_text1 = Text(self.window, background='#ccc')
        self.result_text1.place(x=140, y=10, width=155, height=155)
        self.result_text1.bind("Key-Return>", self.submit)
 
        # 創建一個按鈕
        # 為按鈕添加事件
        self.submit_btn = Button(self.window,
                                 text=u'獲取天氣',
                                 command=self.submit)
        self.submit_btn.place(x=170, y=165, width=70, height=25)
        self.submit_btn2 = Button(self.window, text=u'清空', command=self.clean)
        self.submit_btn2.place(x=250, y=165, width=35, height=25)
 
        # 標題
        self.title_label = Label(self.window, text=u'今日天氣:')
        self.title_label.place(x=10, y=165)
 
        # 結果
        self.result_text = Text(self.window, background='#ccc')
        self.result_text.place(x=10, y=190, width=285, height=165)
 
    def submit(self):
        # 從輸入框獲取用戶輸入的值
        content = self.result_text1.get(0.0, END).strip().replace("\n", " ")
 
        # 把城市信息傳到爬蟲函數中
        result = self.weather.crawl(content)
 
        # 將結果顯示在窗口中的文本框中
        for k, v in result.items():
            self.result_text.insert(END, k + ':' + v)
            self.result_text.insert(END, '\n')
            self.result_text.insert(END, '\n')
 
    # 清空文本域中的內容
    def clean(self):
        self.result_text1.delete(0.0, END)
        self.result_text.delete(0.0, END)
 
    def run(self):
        self.window.mainloop()

運行結果如下:

四、完整代碼

import json
import requests
import re
import tkinter as Tk
from tkinter import Tk, Button, Entry, Label, Text, END
 
 
class Weather(object):
    def __init__(self):
        pass
 
    def crawl(self, key):
 
        url = 'http://weathernew.pae.baidu.com/weathernew/pc?query=' + key + '天氣srcid=4982city_name=' + key + 'province_name=' + key
 
        # 設置請求頭
        headers = {
            'user-agent':
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
            'Referer': 'https://googleads.g.doubleclick.net/'
        }
        # 頁面HTML
        res = requests.get(url, headers=headers).text
        # 時間
        time = re.search(r'\{\"update_time\":\"(.+?)\"', res).group(1)
        # 天氣
        weather = re.search(r'\"weather\"\:\"(.+?)\"', res).group(1)
        weather = weather.encode('utf-8').decode("unicode-escape")
        # 氣溫
        weather_l = re.search(r'temperature_night\"\:\"(.+?)\"', res).group(1)
        weather_h = re.search(r'temperature_day\"\:\"(.+?)\"', res).group(1)
        # 風力
        wind_now = re.search(r'\"wind_power_day\"\:\"(.+?)\"', res).group(1)
        wind_now = wind_now.encode('utf-8').decode("unicode-escape")
        wind_name = re.search(r'\"wind_direction_day\"\:\"(.+?)\"',
                              res).group(1)
        wind_name = wind_name.encode('utf-8').decode("unicode-escape")
        wind = wind_name + wind_now
        # 貼示
        desc = re.search(r'\"desc\"\:\"(.+?)\"', res).group(1)
        desc = desc.encode('utf-8').decode("unicode-escape")
 
        # 結果生
        dic = {
            '城市': key,
            '更新時間': time,
            '天氣': weather,
            '溫度': weather_l + '-' + weather_h + '攝氏度',
            '風力': wind,
            '貼示': desc,
        }
        return dic
 
 
class Weather_UI(object):
    def __init__(self):
        self.window = Tk()
        self.weather = Weather()
 
        self.window.title(u'天氣預報')
        # 設置窗口大小和位置
        self.window.geometry('310x370')
        # 創建一個文本框
        self.result_text0 = Label(self.window, text=u'學長所在城市:\n要寫中文呦')
        self.result_text0.place(x=10, y=5, height=130)
        self.result_text0.bind('提示')
 
        self.result_text1 = Text(self.window, background='#ccc')
        self.result_text1.place(x=140, y=10, width=155, height=155)
        self.result_text1.bind("Key-Return>", self.submit)
 
        # 創建一個按鈕
        # 為按鈕添加事件
        self.submit_btn = Button(self.window,
                                 text=u'獲取天氣',
                                 command=self.submit)
        self.submit_btn.place(x=170, y=165, width=70, height=25)
        self.submit_btn2 = Button(self.window, text=u'清空', command=self.clean)
        self.submit_btn2.place(x=250, y=165, width=35, height=25)
 
        # 標題
        self.title_label = Label(self.window, text=u'今日天氣:')
        self.title_label.place(x=10, y=165)
 
        # 結果
        self.result_text = Text(self.window, background='#ccc')
        self.result_text.place(x=10, y=190, width=285, height=165)
 
    def submit(self):
        # 從輸入框獲取用戶輸入的值
        content = self.result_text1.get(0.0, END).strip().replace("\n", " ")
 
        # 把城市信息傳到爬蟲函數中
        result = self.weather.crawl(content)
 
        # 將結果顯示在窗口中的文本框中
        for k, v in result.items():
            self.result_text.insert(END, k + ':' + v)
            self.result_text.insert(END, '\n')
            self.result_text.insert(END, '\n')
 
    # 清空文本域中的內容
    def clean(self):
        self.result_text1.delete(0.0, END)
        self.result_text.delete(0.0, END)
 
    def run(self):
        self.window.mainloop()
 
 
A = Weather_UI()
A.run()

到此這篇關于Python實現天氣查詢系統的文章就介紹到這了,更多相關Python天氣查詢內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python腳本制作天氣查詢實例代碼
  • python趣味挑戰之爬取天氣與微博熱搜并自動發給微信好友
  • python制作的天氣預報小工具(gui界面)
  • Python爬蟲之獲取心知天氣API實時天氣數據并彈窗提醒
  • Python天氣語音播報小助手

標簽:揚州 迪慶 六盤水 牡丹江 聊城 楊凌 南寧 撫州

巨人網絡通訊聲明:本文標題《Python實現天氣查詢軟件》,本文關鍵詞  Python,實現,天氣查詢,軟件,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Python實現天氣查詢軟件》相關的同類信息!
  • 本頁收集關于Python實現天氣查詢軟件的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 天全县| 涞源县| 太和县| 景德镇市| 丹寨县| 五指山市| 宁武县| 屏山县| 呼和浩特市| 巫溪县| 惠水县| 岗巴县| 浪卡子县| 阳东县| 福贡县| 蕉岭县| 嘉祥县| 南城县| 翁源县| 蒙自县| 金沙县| 日照市| 景德镇市| 巴塘县| 泾川县| 乌兰察布市| 黄骅市| 晴隆县| 武冈市| 樟树市| 莱芜市| 泗阳县| 肥乡县| 石楼县| 简阳市| 陵川县| 中阳县| 浦江县| 东乡| 新闻| 连城县|