在网上爬取天气,做个笔记,应该还有更简单的方法
最后的效果
代码
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())