Python代码优化(1):天气预报获取

天气预报获取模块

基于http://www.weather.com.cn/做了代码优化,每个函数功能更加明确,更加适合单独集成。接口也进行了优化,比网络上博主抄来抄去的源码要好一些。

改进点:获取天气拆成了多个函数,多次保存csv文件不再出现标题栏重写的情况。

天气获取模块

1.1 依赖表
包名含义安装方式
系统包
os
csv
json
第三方依赖
requests爬虫模块
bs4.BeautifulSoup网页解析对象
bs4.element.Tag网页标签对象
1.2 全局变量表
变量名含义初值
1.3 函数
函数名含义是否接口
get_html_text请求网页内容,无代理无head模式
get_today_weather获取当天天气
get_1_7_weather获取当周天气
get_today_and_week获取当周和当天的天气
get_8_14_weather获取下一周天气
write_to_csv保存文件模块
1.4 接口函数
函数名含义
get_two_weak_weather获取两周天气
入参含义类型
city_code城市代码字符串
出参
None
1.5 代码
# weather.py
import os
import csv
import json

import requests
from bs4 import BeautifulSoup
from bs4.element import Tag


def get_html_text(url):
    """请求获得网页内容"""
    try:
        r = requests.get(url, timeout=30)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        print("成功访问")
        return r.text
    except Exception as e:
        print(e)
        print("访问错误")
        return " "


def get_today_weather(body_tag: Tag):
    td_wea_list = []  # 存放当天的数据,list
    count = 0

    def get_today_json(_tag: Tag):
        # 获取今日数据的script
        weather_div = _tag.find_all('div', {'class': 'left-div'})
        observe24h_data = weather_div[2].find('script').string

        # 将 script 数据改变成为 json 数据 (移除 var data=)
        begin_index = observe24h_data.index('=') + 1
        end_index = -2
        observe24h_data = observe24h_data[begin_index: end_index]
        observe24h_json = json.loads(observe24h_data)
        t_json = observe24h_json['od']['od2']

        # 补充空气质量
        if t_json[0]['od28'] == "":
            t_json[0]['od28'] = t_json[1]['od28']

        return t_json

    today_json = get_today_json(body_tag)
    for i in today_json:
        od_wea_list = []
        if count <= 23:
            od_wea_list.append(i['od21'])  # 添加时间
            od_wea_list.append(i['od22'])  # 添加当前时刻温度
            od_wea_list.append(i['od24'])  # 添加当前时刻风力方向
            od_wea_list.append(i['od25'])  # 添加当前时刻风级
            od_wea_list.append(i['od26'])  # 添加当前时刻降水量
            od_wea_list.append(i['od27'])  # 添加当前时刻相对湿度
            od_wea_list.append(i['od28'])  # 添加当前时刻空气质量
            print(od_wea_list)
            td_wea_list.append(od_wea_list)
        count = count + 1
    print(td_wea_list)
    return td_wea_list


def get_1_7_weather(body_tag: Tag):
    week_wea_list = list()  # 初始化一个列表保存数据,返回值
    
    # 解析7天的数据, body -> div -> ul -> ui
    data = body_tag.find('div', {'id': '7d'})
    ul = data.find_all('ul')[0]
    li = ul.find_all('li')

    for day in li:  # 遍历找到的每一个li
        # 临时存放每天的数据
        od_wea_list = list()

        # 获取日期
        date = day.find('h1').string
        date = date[0:date.index('日')]
        od_wea_list.append(date)

        # 找出li下面的p标签,有三个p标签,分别为天气,气温,风向
        inf = day.find_all('p')

        # 提取第一个p标签的值,即天气
        od_wea_list.append(inf[0].string)

        # 提取第二个p标签的值,最高气温和最低气温
        if inf[1].find('i') is None:
            tem_low = None
        else:
            tem_low = inf[1].find('i').string  # 找到最低气温

        if inf[1].find('span') is None:  # 天气预报可能没有最高气温
            tem_high = None
        else:
            tem_high = inf[1].find('span').string  # 找到最高气温

        if tem_low[-1] == '℃':
            od_wea_list.append(tem_low[:-1])
        else:
            od_wea_list.append(tem_low)

        if tem_high[-1] == '℃':
            od_wea_list.append(tem_high[:-1])
        else:
            od_wea_list.append(tem_high)

        # 提取第三个p标签的值,初始风向和转风向,风级
        wind = inf[2].find_all('span')  # 找到风向
        for j in wind:
            od_wea_list.append(j['title'])

        wind_scale = inf[2].find('i').string  # 找到风级
        index1 = wind_scale.index('级')
        od_wea_list.append(int(wind_scale[index1 - 1:index1]))

        # 添加日志
        week_wea_list.append(od_wea_list)
        # print(week_wea_list)
    return week_wea_list

    
def get_today_and_week(html: str):
    """处理得到有用信息保存数据文件"""
    bs = BeautifulSoup(html, "html.parser")  # 创建BeautifulSoup对象
    body = bs.body

    # 获取当天数据
    td_wea_list = get_today_weather(body)
    
    # 获取七天内的数据
    week_wea_list = get_1_7_weather(body)

    return td_wea_list, week_wea_list


def get_8_14_weather(html):
    week_wea_list = []
    i = 0  # 控制爬取的天数

    bs = BeautifulSoup(html, "html.parser")  # 创建BeautifulSoup对象
    body = bs.body

    data = body.find('div', {'id': '15d'})  # 找到div标签且id = 15d
    ul = data.find('ul')  # 找到所有的ul标签
    li = ul.find_all('li')  # 找到左右的li标签

    for day in li:  # 遍历找到的每一个li
        if i < 7:
            od_wea_list = list()

            # 获取日期
            date = day.find('span', {'class': 'time'}).string  # 得到日期
            date = date[date.index('(') + 1:-2]  # 取出日期号
            od_wea_list.append(date)

            # 获取天气
            weather = day.find('span', {'class': 'wea'}).string  # 找到天气
            print(day.find('span', {'class': 'wea'}).string)
            print(day.find('span', {'class': 'wea'}).text)
            od_wea_list.append(weather)

            # 获取温度
            tem = day.find('span', {'class': 'tem'}).text  # 找到温度
            print(tem)
            od_wea_list.append(tem[tem.index('/') + 1:-1])  # 找到最低气温
            od_wea_list.append(tem[:tem.index('/') - 1])  # 找到最高气温

            # 获取风向和风级
            wind = day.find('span', {'class': 'wind'}).string  # 找到风向
            if '转' in wind:  # 如果有风向变化
                od_wea_list.append(wind[:wind.index('转')])
                od_wea_list.append(wind[wind.index('转') + 1:])
            else:  # 如果没有风向变化,前后风向一致
                od_wea_list.append(wind)
                od_wea_list.append(wind)

            wind_scale = day.find('span', {'class': 'wind1'}).string  # 找到风级
            index1 = wind_scale.index('级')
            od_wea_list.append(int(wind_scale[index1 - 1:index1]))

            week_wea_list.append(od_wea_list)
    return week_wea_list


def write_to_csv(file_name, data, day=14):
    """保存为csv文件"""
    if not os.path.exists(file_name):
        with open(file_name, 'w', errors='ignore', newline='') as f:
            if day == 14:
                header = ['日期', '天气', '最低气温', '最高气温', '风向1', '风向2', '风级']
            else:
                header = ['小时', '温度', '风力方向', '风级', '降水量', '相对湿度', '空气质量']
            f_csv = csv.writer(f)
            f_csv.writerow(header)
            f_csv.writerows(data)

    else:
        with open(file_name, 'a', errors='ignore', newline='') as f:
            f_csv = csv.writer(f)
            f_csv.writerows(data)


def get_two_weak_weather(city_code: str):
    url_head = "http://www.weather.com.cn/weather"
    url_1_7 = "".join([url_head, "/", city_code, ".shtml"])
    url_8_14 = "".join([url_head, "15d", "/", city_code, ".shtml"])

    html_1_7 = get_html_text(url_1_7)
    data1, data1_7 = get_today_and_week(html_1_7)

    html8_14 = get_html_text(url_8_14)
    data8_14 = get_8_14_weather(html8_14)
    data14 = data1_7 + data8_14

    write_to_csv('weather14.csv', data14, 14)  # 保存为csv文件
    write_to_csv('weather1.csv', data1, 1)


if __name__ == '__main__':
    get_two_weak_weather("101280701")

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以参考下面的代码示例,使用 Python获取天气预报:import requests# Get the response from OpenWeatherMap response = requests.get('http://api.openweathermap.org/data/2.5/weather?q=London,uk')# Convert the response to a Python dictionary weather_data = response.json()# Get the temperature from the dictionary temperature = weather_data['main']['temp'] ### 回答2: 下面是一个示例代码,可以使用Python获取天气预报: ```python import requests def get_weather(city): # 构造请求的URL url = f"http://api.weatherapi.com/v1/forecast.json?key=YOUR_API_KEY&q={city}&days=1" try: # 发送GET请求获取天气数据 response = requests.get(url) data = response.json() # 解析天气预报数据 current = data['current'] condition = current['condition']['text'] temp_c = current['temp_c'] wind_kph = current['wind_kph'] # 打印天气信息 print(f"城市:{city}") print(f"天气状况:{condition}") print(f"当前温度:{temp_c} 度") print(f"风速:{wind_kph} 公里/小时") except: print("获取天气信息失败!") # 调用函数获取天气预报 city = input("请输入城市名:") get_weather(city) ``` 需要注意的是,上面的代码使用了第三方的天气API,你需要替换`YOUR_API_KEY`为你自己的API密钥。此外,该代码只获取了当前的天气信息,如果需要获取更多天气预报的数据,可以根据API文档进行相应的修改。 ### 回答3: 以下是一个使用Python获取天气预报的代码示例: ```python import requests def get_weather(city): # 构建请求url url = f"http://api.weatherapi.com/v1/current.json?key={YOUR_API_KEY}&q={city}" try: # 发送请求并获取响应 response = requests.get(url) result = response.json() # 解析响应数据 weather = result["current"]["condition"]["text"] temperature = result["current"]["temp_c"] # 打印天气信息 print(f"{city}的天气为:{weather}") print(f"当前温度为:{temperature}摄氏度") except requests.exceptions.RequestException as e: # 请求异常处理 print("请求出错:", e) # 调用函数获取天气信息 get_weather("北京") ``` 请注意,在上面的代码中,我们使用了一个名为`requests`的第三方库,它负责发送HTTP请求并处理响应。我们还需要替换代码中`YOUR_API_KEY`的部分为您自己的天气API密钥。此外,您还需要安装`requests`库,可以通过`pip install requests`命令进行安装。当运行代码时,它将获取指定城市的天气预报,并打印出天气和当前温度信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值