代码拉取完成,页面将自动刷新
import requests
import re,time
url = "https://cdn.heweather.com/china-city-list.txt"
res = requests.get(url)
data = res.content.decode("utf-8")
dlist = re.split("[\n\r]+",data)
for a in range(5):
dlist.remove(dlist[0])
for i in range(2):
#使用空白符拆分出每个字段信息
item = re.split("\s+",dlist[i])
#输出
#print(item)
#print(item[1],":",item[3]," ",item[5])
url = "https://free-api.heweather.com/s6/weather?location=%s&key=4efa3cc470704500980d345a962fbe5c"%(item[5])
res = requests.get(url)
time.sleep(2)
datalist = res.json()
data = datalist['HeWeather6'][0]
#输出天气信息
print("城市:", data['basic']['location'])
print("时间:", str(data['daily_forecast'][0]['date']))
print("温度:", data["daily_forecast"][0]["tmp_min"],'~',data["daily_forecast"][0]["tmp_max"])
print(data['daily_forecast'][0]['cond_txt_d']," . ",data['daily_forecast'][0]['cond_txt_n'])
print(data['daily_forecast'][0]['wind_dir'], data['daily_forecast'][0]['wind_sc'], '级')
print('建议:',data["lifestyle"][0]["txt"])
print("-"*60)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。