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)
正常情况下结果是这样的: