基于qqbot做一个qq自动查询天气机器人

qqbot的github页面:https://github.com/pandolia/qqbot

原理就是通过爬虫爬取天气信息,然后通过onQQMessage相应函数相应请求

qqbot的安装 :在cmd里输入pip install qqbot即可

启动qqbot:在cmd里直接输入qqbot

使用插件:在另一个cmd里键入 qq plug sample 

卸载插件: 在第二个cmd里键入  qq unplug sample

PS:sample就是你脚本的名字

写一个相应脚本 sample.py  (叫别的名字也行),放到 C:\Users\junk beat\.qqbot-tmp\plugins 这个目录下

脚本里面包含了查天气的爬虫,api说明页面 

https://www.sojson.com/blog/305.html

爬虫代码如下:

def get_weather(city):
    ##返回一个今天信息的列表和未来4天信息的列表
    import requests
    import json
    r = city
    #天津是的id是101030100
    #https://www.sojson.com/blog/305.html
    url = "http://t.weather.sojson.com/api/weather/city/101030100"
    result = requests.get(url).json()  #将字符串转换为字典
    if result.get('status') != 200:
        print("请求出错,错误代码%d\n" % result.get('status'))
        return -1
    time = result["time"]   #系统时间
    update_time = result.get("cityInfo").get("updateTime")  #城市天气预报更新时间
    humidness = result.get("data").get("shidu")              #获得当前湿度
    pm25 = result.get("data").get("pm25")                    #pm2.5
    pm10 = result.get("data").get("pm10")
    quality = result.get("data").get("quality")              #空气质量
    temperature = result.get("data").get("wendu")            #气温
    today = []                   #记录今天信息的列表
    today.append(time)
    today.append(update_time)
    today.append(humidness)
    today.append(pm25)
    today.append(pm10)
    today.append(quality)
    today.append(temperature)
    print(today)
    data = [None]*4
    sunrise = [None]*4
    high = [None]*4
    low = [None]*4
    sunset = [None]*4
    aqi = [None]*4
    fx = [None]*4
    fl = [None]*4
    type = [None]*4
    notice = [None]*4

    """
    forcast[]消息样例
     "date": "22日星期六",
                "sunrise": "05:57",
                "high": "高温 26.0℃",
                "low": "低温 15.0℃",
                "sunset": "18:10",
                "aqi": 55.0,
                "fx": "西北风",
                "fl": "4-5级",
                "type": "晴",
                "notice": "愿你拥有比阳光明媚的心情"
    """
    result_data = result["data"]["forecast"]
    dict = {}
    for i in range(0, 4):
        dict[i] = result_data[i + 1]
        data[i] = dict[i].get("data")
        sunrise[i] = dict[i].get("sunrise")
        high[i] = dict[i].get("high")
        low[i] = dict[i].get("low")
        sunset[i] = dict[i].get("sunset")
        aqi[i] = dict[i].get("aqi")
        fx[i] = dict[i].get("fx")
        fl[i] = dict[i].get("fl")
        type[i] = dict[i].get("type")
        notice[i] = dict[i].get("notice")
    info = []                      #记录未来4天信息的列表
    info.append(data)
    info.append(sunrise)
    info.append(high)
    info.append(low)
    info.append(sunset)
    info.append(aqi)
    info.append(fx)
    info.append(fl)
    info.append(type)
    info.append(notice)
    print(info)
    return today,info

if __name__ == '__main__':
    today , info = get_weather("天津")
    print(today)
    print(info)

正常情况下结果是这样的:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值