Python群机器人发送城市天气情况

方法1:获取页面HTML内容,再通过正则表达式来获取需要的内容

#!/usr/bin/env python
#coding=gbk
import requests
import urllib.request ,sys
import re
import time

def get_weather(provice,city):
    url="http://qq.ip138.com/weather/"+provice+'/'+city+'.htm'
    wetherhtml=urllib.request.urlopen(url)
    result=wetherhtml.read().decode('gb2312')
    f=open('weather.txt','wb')
    f.write(result.encode('gb2312'))
    f.close()

    regex1='>(\d*-\d*-\d*.+?)<'
    date=re.findall(regex1,result)

    regex2='alt="(.+?)" />'
    weather=re.findall(regex2,result)

    regex3='<td>(\d{1,2}.+)</td>'
    temperature=re.findall(regex3,result)

    length=len(date)
    t="未来5天天气预报:\n"
    msg=''
    for i in range(length):
        msg= msg+date[i]+" 天气:"+weather[i]+", 温度:<font color=\"warning\">"+temperature[i]+"</font>\n";
    return(t+msg)


#文字标记(markdown)机器人
def tony_bot_txt_markdown(boturl,provice,city,cityname):
    weather="下雨"
    temperature="20度"
    headers={"Content-Type":"text/plain"}
    city=get_weather(provice,city)
    data={
        "msgtype": "markdown",
 
        "markdown": {

        "content": cityname+city
        }
}
    res=requests.post(url=boturl,headers=headers,json=data)

#测试
tony_bot_txt_markdown('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx','hunan','zhuzhou','株洲')

方法2:如果内容是json等规范格式的情况

{"data":{"yesterday":{"date":"17日星期日","high":"高温 11℃","fx":"西北风","low":"低温 -4℃","fl":"<![CDATA[5-6级]]>","type":"多云"},"city":"北京","forecast":[{"date":"18日星期一","high":"高温 4℃","fengli":"<![CDATA[3-4级]]>","low":"低温 -5℃","fengxiang":"西北风","type":"晴"},{"date":"19日星期二","high":"高温 6℃","fengli":"<![CDATA[<3级]]>","low":"低温 -4℃","fengxiang":"西南风","type":"晴"},{"date":"20日星期三","high":"高温 7℃","fengli":"<![CDATA[<3级]]>","low":"低温 -3℃","fengxiang":"北风","type":"晴"},{"date":"21日星期四","high":"高温 9℃","fengli":"<![CDATA[<3级]]>","low":"低温 -1℃","fengxiang":"北风","type":"晴"},{"date":"22日星期五","high":"高温 10℃","fengli":"<![CDATA[<3级]]>","low":"低温 0℃","fengxiang":"东南风","type":"多云"}],"ganmao":"将有一次强降温过程,天气寒冷,且风力较强,极易发生感冒,请特别注意增加衣服保暖防寒。","wendu":"-1"},"status":1000,"desc":"OK"}

#coding=gbk
import urllib.request
import gzip
import json

def get_weather():
    city_name = input('请输入城市名称:')
    url = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name)
    w_data = urllib.request.urlopen(url).read()
    w_data = gzip.decompress(w_data).decode('utf-8')
    w_dict = json.loads(w_data)
    return w_dict

wdata=get_weather();
if(wdata.get('desc')=='OK'):
    cityName=wdata.get('data').get('city')
    wendu=wdata.get('data').get('wendu')+"℃"
    tips=wdata.get('data').get('ganmao')
    forecast=wdata.get('data').get('forecast')
    today_date=forecast[0].get('date')
    today_high=forecast[0].get('high')
    today_fengli=forecast[0].get('fengli')
    print(today_fengli.replace('<![CDATA[','').replace(']]>',''))#<![CDATA[<3级]]>转成<3级
    isDisplay=input('是否显示未来4天的天气情况:')
    if(isDisplay=='是' or isDisplay=='Y' or isDisplay=='y'):
        for i in range(1,5):
            print(forecast[i].get('date')+','+forecast[i].get('high')+','+forecast[i].get('low')) 
else:
    print('输入的城市名称有错误!')

方法2的简化版

#coding=gbk
import json
import requests

whtml = requests.get('http://wthrcdn.etouch.cn/weather_mini?city=株洲')
wdata = json.loads(whtml.text)

print(wdata['data'])
print("\n")
for i in range(len(wdata['data']['forecast'])):
    print(wdata['data']['forecast'][i]['type'])

pip install -i https://pypi.douban.com/simple pyinstaller
pyinstaller -F -w -i xxx.ico yy.py
在dist目录下面生成exe文件

发布了46 篇原创文章 · 获赞 9 · 访问量 3642

猜你喜欢

转载自blog.csdn.net/weixin_41896770/article/details/103120160