Method 1: Obtain HTML page content, and then to get the desired content by regular expressions
#!/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','株洲')
Method 2: If the content is the case json specification formats
fengli ":"! <[CDATA [<stage 3]]> "," low ":" Low Temperature -3 ℃ "," fengxiang ":" north "," type ":" clear "}, {" date " : "Thursday 21", "high": "high temperature 9 ℃", "fengli": "! <[CDATA [<stage 3]]>", "low": "low temperature -1 ℃", "fengxiang": "north", "type": "clear"}, { "date": "Friday 22", "high": "high temperature 10 ℃", "fengli":! "<[CDATA [<stage 3]] > "," low ":" low Temperature 0 ℃ "," fengxiang ":" southeast wind "," type ":" cloudy "}]," ganmao ":" we will have a strong cooling process, cold, and wind than strong, cold easily, please pay special attention to increase clothes warm cold. "," Wendu ":" - 1 "}," status ": 1000," desc ":" OK "} Low Temperature -3 ℃ "," fengxiang ":" north "," type ":" clear "}, {" date ":" Thursday 21 "," high ":" high temperature 9 ℃ "," fengli ":" ! <[CDATA [<stage 3]]> "," low ":" low Temperature -1 ℃ "," fengxiang ":" north "," type ":" clear "}, {" date ":" 22 Day Friday "," high ":" high temperature 10 ℃ "," fengli ":" <[CDATA [<stage 3]]> "," low! ":" low temperature 0 ℃ "," fengxiang ":" southeast wind " "type": "cloudy"}], "ganmao": "there will be a strong cooling process, cold weather and strong wind, cold easily, please pay special attention to increase clothes warm cold. "," Wendu ":" - 1 "}," status ": 1000," desc ":" OK "} Low Temperature -3 ℃ "," fengxiang ":" north "," type ":" clear "}, {" date ":" Thursday 21 "," high ":" high temperature 9 ℃ "," fengli ":" ! <[CDATA [<stage 3]]> "," low ":" low Temperature -1 ℃ "," fengxiang ":" north "," type ":" clear "}, {" date ":" 22 Day Friday "," high ":" high temperature 10 ℃ "," fengli ":" <[CDATA [<stage 3]]> "," low! ":" low temperature 0 ℃ "," fengxiang ":" southeast wind " "type": "cloudy"}], "ganmao": "there will be a strong cooling process, cold weather and strong wind, cold easily, please pay special attention to increase clothes warm cold. "," Wendu ":" - 1 "}," status ": 1000," desc ":" OK "} Temperature 9 ℃ "," fengli ":"! <[CDATA [<stage 3]]> "," low ":" Low Temperature -1 ℃ "," fengxiang ":" north "," type ":" clear " }, { "date": "Friday 22", "high": "high temperature 10 ℃", "fengli": "! <[CDATA [<stage 3]]>", "low": "low temperature 0 ℃" , "fengxiang": "South East wind", "type": "cloudy"}], "ganmao": "there will be a strong cooling process, cold weather and strong wind, cold easily, please pay special attention to increase in clothing warm cold. "," Wendu ":" - 1 "}," status ": 1000," desc ":" OK "} Temperature 9 ℃ "," fengli ":"! <[CDATA [<stage 3]]> "," low ":" Low Temperature -1 ℃ "," fengxiang ":" north "," type ":" clear " }, { "date": "Friday 22", "high": "high temperature 10 ℃", "fengli": "! <[CDATA [<stage 3]]>", "low": "low temperature 0 ℃" , "fengxiang": "South East wind", "type": "cloudy"}], "ganmao": "there will be a strong cooling process, cold weather and strong wind, cold easily, please pay special attention to increase in clothing warm cold. "," Wendu ":" - 1 "}," status ": 1000," desc ":" OK "} "," Low ":" Low Temperature 0 ℃ "," fengxiang ":" southeast wind "," type ":" cloudy "}]," ganmao ":" will have a strong cooling process, cold weather, and strong winds , cold easily, please pay special attention to increase clothes warm cold. "," Wendu ":" - 1 "}," status ": 1000," desc ":" OK "} "," Low ":" Low Temperature 0 ℃ "," fengxiang ":" southeast wind "," type ":" cloudy "}]," ganmao ":" will have a strong cooling process, cold weather, and strong winds , cold easily, please pay special attention to increase clothes warm cold. "," 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('输入的城市名称有错误!')
Method 2 Lite
#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'])
-i https://pypi.douban.com/simple pyinstaller the install PIP
pyinstaller -F -w -i xxx.ico yy.py
generated exe file below the directory dist