python3获取网页天气预报信息并打印

查到一个可以提供区域天气预报的url,说明文档在https://www.sojson.com/blog/234.html,

https://www.sojson.com/open/api/weather/json.shtml?city=%E6%88%90%E9%83%BD

打算用python从上面获取预报信息,打印下来

代码如下:
'''
想要学习Python?Python学习交流群:1004391443满足你的需求,资料都已经上传群文件,可以自行下载!
'''
import urllib.request
import time
import ssl
import json
# #weather = "https://www.baidu.com"
time.sleep(3)    #此处等待3秒主要是对应网页提示,三秒内只能访问一次
ssl._create_default_https_context = ssl._create_unverified_context
url = r"https://www.sojson.com/open/api/weather/json.shtml?city=%E6%88%90%E9%83%BD"#一个提供区域天气预报的url



page = urllib.request.urlopen(url)
# # ssl._create_default_https_context=ssl._create_unverified_context
html = page.read().decode("utf-8")


'''
    json.dumps()和json.loads()是json格式处理函数(可以这么理解,json是字符串)
    (1)json.dumps()函数是将一个Python数据类型列表进行json格式的编码(可以这么理解,json.dumps()函数是将字典转化为字符串)
    (2)json.loads()函数是将json格式数据转换为字典(可以这么理解,json.loads()函数是将字符串转化为字典)
'''

res = json.loads(html)
a = json.dumps(res, ensure_ascii=False, indent=4)          #将读取到的内容格式化,这样就可以看到有缩进、换行的内容
# print(a)
fp = open(r"C:\Users\xxx\Documents\weather.txt", "w",encoding='UTF-8') #将读取内容保存到文件
fp.write(a) #写入数据
fp.close() #关闭文件

res = json.loads(a) #将json转化为dict
# print(res)

'''
通过查看抓到的代码,发现dict中嵌套了dict,所以需要把对应的dict取出来
同样,forecast中,在list里嵌套了dict,需要仔细查看并设置中间变量
'''
today = res['data']
yesterday = today['yesterday']
forcast = today['forecast']
tomorrow = forcast[0]      #注意看res文件内容,forecast其实是一个list,其元素才是dict

print("\n\n")
print("===============今日天气===============")
print("日期:       ",res['date'])
print("城市:       ",res['city'])
print("温度:       ",today['wendu'])
print("湿度:       ",today['shidu'])
print("PM2.5:     ",today['pm25'])
print("空气质量:    ",today['quality'])

print("\n\n")
print("===============昨日天气===============")
print("日期:          ",yesterday['date'])
print("城市:          ",res['city'])
print("最高温度:       ",yesterday['high'])
print("最低温度:       ",yesterday['low'])
print("天气状况:       ",yesterday['type'])
# print("PM2.5:     ",today['pm25'])
# print("空气质量:    ",today['quality'])

print("\n\n")
print("===============明日预报===============")
print("日期:          ",tomorrow['date'])
print("城市:          ",res['city'])
print("最高温度:       ",tomorrow['high'])
print("最低温度:       ",tomorrow['low'])
print("天气状况:       ",tomorrow['type'])
print("温馨提醒:       ",tomorrow['notice'])
# print("PM2.5:     ",today['pm25'])
# print("空气质量:    ",today['quality'])





'''
下面是文件中的全部内容,主要是分清楚dict和list的层次,设置好中间变量,取出对应元素
{
    "date": "20180426",
    "message": "Success !",
    "status": 200,
    "city": "成都",
    "count": 627,
    "data": {
        "shidu": "77%",
        "pm25": 19.0,
        "pm10": 40.0,
        "quality": "优",
        "wendu": "17",
        "ganmao": "各类人群可自由活动",
        "yesterday": {
            "date": "25日星期三",
            "sunrise": "06:28",
            "high": "高温 22.0℃",
            "low": "低温 14.0℃",
            "sunset": "19:37",
            "aqi": 34.0,
            "fx": "无持续风向",
            "fl": "<3级",
            "type": "阴",
            "notice": "不要被阴云遮挡住好心情"
        },
        "forecast": [
            {
                "date": "26日星期四",
                "sunrise": "06:27",
                "high": "高温 25.0℃",
                "low": "低温 15.0℃",
                "sunset": "19:38",
                "aqi": 51.0,
                "fx": "无持续风向",
                "fl": "<3级",
                "type": "多云",
                "notice": "阴晴之间,谨防紫外线侵扰"
            },
            {
                "date": "27日星期五",
                "sunrise": "06:26",
                "high": "高温 27.0℃",
                "low": "低温 16.0℃",
                "sunset": "19:39",
                "aqi": 79.0,
                "fx": "无持续风向",
                "fl": "<3级",
                "type": "多云",
                "notice": "阴晴之间,谨防紫外线侵扰"
            },
            {
                "date": "28日星期六",
                "sunrise": "06:25",
                "high": "高温 26.0℃",
                "low": "低温 17.0℃",
                "sunset": "19:39",
                "aqi": 79.0,
                "fx": "无持续风向",
                "fl": "<3级",
                "type": "多云",
                "notice": "阴晴之间,谨防紫外线侵扰"
            },
            {
                "date": "29日星期日",
                "sunrise": "06:24",
                "high": "高温 27.0℃",
                "low": "低温 18.0℃",
                "sunset": "19:40",
                "aqi": 71.0,
                "fx": "无持续风向",
                "fl": "<3级",
                "type": "多云",
                "notice": "阴晴之间,谨防紫外线侵扰"
            },
            {
                "date": "30日星期一",
                "sunrise": "06:23",
                "high": "高温 25.0℃",
                "low": "低温 17.0℃",
                "sunset": "19:41",
                "aqi": 66.0,
                "fx": "无持续风向",
                "fl": "<3级",
                "type": "小雨",
                "notice": "雨虽小,注意保暖别感冒"
            }
        ]
    }
}

'''

主要思路是:

1,先从url获取信息;

2,将信息格式化,转换为dict;

3,从dict中取出对应的项

注意网络连接,目标网址会识别同一IP的访问频率,三秒以内只允许一次

关键步骤是,从url获取信息后,得到的内容是没有换行和缩进的,即非格式化数据,所以需要使用json.dumps和json.loads将数据格式化

备注:

    json.dumps()和json.loads()是json格式处理函数(可以这么理解,json是字符串)

    (1)json.dumps()函数是将一个Python数据类型列表进行json格式的编码(可以这么理解,json.dumps()函数是将字典转化为字符串

    (2)json.loads()函数是将json格式数据转换为字典(可以这么理解,json.loads()函数是将字符串转化为字典)

你可能感兴趣的:(Python)