python 爬天气

在网上爬取天气,做个笔记,应该还有更简单的方法

最后的效果 

代码

import re
from datetime import datetime
import requests
from bs4 import BeautifulSoup

now_date = datetime.today().strftime('%Y-%m-%d %H:%M')
today_date = datetime.today().strftime('%Y-%m-%d')
url = 'http://www.weather.com.cn/weather/101270101.shtml'
resp = requests.get(url)
html = resp.content.decode('utf-8')


def weather_call():
    #  3:提取内容
    rule_time = r'(?<=<h1>)\d\d日.*(?=</h1>)'
    time_list = re.findall(rule_time, html)  # ['<h1>19日(今天)</h1>'
    rule_temp = r'<span>.*</span>/<i>.*</i>'  # <span>31℃</span>/<i>23℃</i>或# '<span>31</span>/<i>22℃</i>'
    tem_list = re.findall(rule_temp, html)
    temp_list = []
    for temp in tem_list:
        temp1 = re.sub('</?\w*>', '', temp)
        temp_list.append(temp1)
    print(temp_list)
    rule_weat = r'(?<=class="wea">).*?(?=</p>)'
    weat_list = re.findall(rule_weat, html)

    week_list = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
    dayweek = week_list[datetime.today().weekday()]
    city = re.findall(r'<title>[\u4e00-\u9fa5]*', html)
    #  print(city[0][7:9])

    #  4:输出
    weather_info = now_date + '\n'
    weather_info = weather_info + '数据来源:中国天气网' + '\n'
    weather_info = weather_info + ('🌈' + city[0][7:9] + '天气提醒🌈').center(35, '-') + '\n'
    weather_info = weather_info + dayweek + '\n' + '温度🌡️:' + temp_list[0] + '\n'
    weather_info = weather_info + weat_list[0] + '\n'
    weather_info = weather_info + '🌈未来三天天气情况🌈'.center(33, '-') + '\n'
    for n in range(3):
        weather_info = weather_info + (
                time_list[n + 1] + '温度:' + str(temp_list[n + 1] + ' 天气状况:' + weat_list[n + 1])) + '\n'

        # 5 保存

    with open('C:\\Users\\User\\Desktop\\天气预报-{}.txt'.format(today_date), 'w', encoding='utf-8') as file:
        file.write(weather_info)
    return weather_info


weather_call()


def tips():
    soup = BeautifulSoup(html, 'html.parser')
    title = '---------生活指数------------'
    tips = soup.find('div', 'hide show').find_all('li')
    for tip in tips:
        em = tip.find('em')
        span = tip.find('span')
        p = tip.find('p')
        title = title + '\n' + ('*' + em.text + '\t\t' + '>' + span.text + '\n' + p.text)

    return title


print(weather_call())
print(tips())

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值