python 43行 写一个天气查询爬虫+GUI图形界面化

这个爬虫爬的是 墨迹天气 https://tianqi.moji.com/weather/china/henan/xinxiang
分析了一下这个网址,不同城市的网址就后边的的拼音不同,这时候就只需拼接用户输入的网址就可得到要查询的网址,然后通过过滤得到天气信息。
顺便要提一下的是这里用的是requests模块和pypinyin模块:
这里选用的是requests模块,个人觉得requests比urllib好用一些,方法也比较简单,具体的就不再说了,可以指及找一下视频或者文档,
还有一个就是pypinyi模块,这里我只是引用一下,没有深入了解,刚开始我调试的时候提示用户输入汉语拼音,总觉得不好,就搜了一下这个模块,不那么反人类哈哈😂😂😂。

import requests
import re
import pypinyin
import tkinter as t
from tkinter import messagebox

header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'
}  
url1 = 'https://tianqi.moji.com/weather/china/henan/'
#  如果想查询别的省或者市、区一行要把这里边的henan的拼音改为你要查询的省

class tianqi:
    def __init__(self):

        root = t.Tk()
        root.title('河南省天气查询')
        root.geometry('400x400+500+100')
        t.Label(root,text='请输入你要查询的城市\n然后点击查询按钮:',font='14').pack(pady=10)
        self.entry = t.Entry(root, width=20, font='14')
        self.entry.pack(padx=9,pady=30)
        t.Button(root,height=3,width=10,text='查询',command=self.chatianqi).pack(pady=50)
        root.mainloop()

    def chatianqi(self):
        self.hp()
        url = url1+s
        response = requests.get(url, headers=header).text
        rule1 = re.compile(r'<meta name="description" content="(.*?)">')
        text = re.search(rule1, response).group()
        newtext = re.sub('墨迹天气', '', text)
        newtext = re.sub('<meta name="description" content="', "", newtext)
        newtext = re.sub('">', '', newtext)
        newtext = re.sub('墨迹天气', '', newtext)
        messagebox.showinfo(title=self.entry.get(),message=newtext)
    def hp(self):   #汉语转汉语拼音方法
        global s
        s = ''
        for i in pypinyin.pinyin(self.entry.get(), style=pypinyin.NORMAL):
            s += ''.join(i)
        return s

tianqi()

运行结果:
在这里插入图片描述
过几天又看了一遍做了个改进版:

这里我比上边的多了两个模块play sound和os模块主要用来读音频和删除音频,合成语音调用的是百度语音合成。
play sound是一个简洁的模块,直接调用play sound方法就能播放。
百度语音合成需要先去百度AI开放平台申请账号进行操作具体请参照官方文档:baidu语音合成在线api接口文档
另外附上一个使用的讲解,简单易懂:https://blog.csdn.net/qq_38161040/article/details/91906442

import requests
import re
import pypinyin
import tkinter as t
import playsound
import os


header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'
}
url1 = 'https://tianqi.moji.com/weather/china/henan/'


class tianqi:
    def __init__(self):

        root = t.Tk()
        root.title('河南省天气查询')
        root.geometry('400x400+500+100')
        t.Label(root,text='请输入你要查询的城市\n然后点击查询按钮:',font='14').pack(pady=10)
        self.entry = t.Entry(root, width=20, font='14')
        self.entry.pack(padx=9,pady=30)
        t.Button(root,height=3,width=10,text='查询',command=self.chatianqi).pack(pady=50)
        root.mainloop()

    def chatianqi(self):
        self.hp()
        url = url1+s
        response = requests.get(url, headers=header).text
        rule1 = re.compile(r'<meta name="description" content="(.*?)">')
        text = re.search(rule1, response).group()
        newtext = re.sub('墨迹天气', '', text)
        newtext = re.sub('<meta name="description" content="', "", newtext)
        newtext = re.sub('">', '', newtext)
        newtext = re.sub('墨迹天气', '', newtext)
        # 做一个自动语音播报
        url0 = 'https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=XGc8jddcjbuYQwiGGkiVyTNp&client_secret=SWOzWBEsDDpE5LS9IGL8TCgvthDVpGwC'
        zidian = eval(requests.get(url0).text)
        if 'audio_tts_post' in zidian["scope"]:
            data = {   #百度语音合成api必要参数
                'tex': newtext,
                'cuid': 'B4-69-21-BE-3F-**',
                "ctp": 1,
                'lan': 'zh',
                'tok': zidian['access_token'],
                # 'per': 111
            }

            url2 = 'https://tsn.baidu.com/text2audio?'

            html = requests.post(url2, data) #合成音频
            audio = html.content  #获取音频

            f = open(s+'.mp3', "wb")
            f.write(audio)
            f.close()    #保存音频

            playsound.playsound(s+'.mp3')   #播放音频
            os.remove(s+'.mp3')   #删除音频方便重复查询
    def hp(self):   #汉字转汉语拼音
        global s
        s = ''
        for i in pypinyin.pinyin(self.entry.get(), style=pypinyin.NORMAL):
            s += ''.join(i)
        return s

if __name__ == '__main__':
    tianqi()
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

super_vab

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值