元旦假期朋友圈被大雪刷爆了,遗憾的是我出去旅行了没有看到武汉的第一场大雪~
夜晚大雪.jpg
查看了武汉近7日的天气,希望还能遇上一场大雪,能愉快的堆雪人、打雪仗~
http://www.weather.com.cn/weather/101200101.shtml
虽然用手机查询天气很方便,但写代码更有成就感。我用python中的Beautiful Soup库爬取信息,代码如下:
首先导入需要的库
import requests
import csv
from bs4 import BeautifulSoup
from requests.exceptions import RequestException #捕获异常
获取需要的数据
需求数据.png
def get_content(url , data = None):
try:
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36'
}
rep = requests.get(url, headers=header)
rep.encoding = 'utf-8'
if rep.status_code==200:
return rep.text
except RequestException:
return '请求异常'
def get_data(html_text):
final = []
bs = BeautifulSoup(html_text, "html.parser")
body = bs.body
data = body.find('div', {'id': '40d'})
ul = data.find('ul')
li = ul.find_all('li')
for day in li:
temp = []
date = day.find('h1').string
temp.append(date)
inf = day.find_all('p')
temp.append(inf[0].string,)
if inf[1].find('span') is None:
temperature_highest = None # 天气预报可能没有当天的最高气温,需要加个判断语句,来输出最低气温
else:
temperature_highest = inf[1].find('span').string
temperature_highest = temperature_highest.replace('℃', '') # 到了晚上网站会变,最高温度后面也有个℃
temperature_lowest = inf[1].find('i').string
temperature_lowest = temperature_lowest.replace('℃', '') # 最低温度后面有个℃,去掉这个符号
temp.append(temperature_highest)
temp.append(temperature_lowest)
final.append(temp)
return final
写入文件
def write_data(data, name):
file_name = name
with open(file_name, 'a', errors='ignore', newline='') as f:
f_csv = csv.writer(f)
f_csv.writerows(data)
if __name__ == '__main__':
url ='http://www.weather.com.cn/weather/101200101.shtml'
html = get_content(url)
result = get_data(html)
write_data(result, '2.weather.csv')
运行后输出的表格如下:
武汉近7日天气.png
网友评论