本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。
(https://weather.mipang.com/)
import requests import parsel import csv import time
url = ‘https://weather.mipang.com/changsha/{}yuefen.html‘.format(page) headers = { ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36‘ } response = requests.get(url=url, headers=headers)
selector = parsel.Selector(response.text) trs = selector.css(‘.tb tr‘) for tr in trs: dit = {} date = tr.css(‘td:nth-child(1)::text‘).get() dit[‘日期‘] = date max_temperature = tr.css(‘td:nth-child(2)::text‘).get() dit[‘最高温度‘] = max_temperature min_temperature = tr.css(‘td:nth-child(3)::text‘).get() dit[‘最低温度‘] = min_temperature weather = tr.css(‘td:nth-child(4)::text‘).get() dit[‘天气‘] = weather wind = tr.css(‘td:nth-child(5)::text‘).get() dit[‘风向‘] = wind wind_power = tr.css(‘td:nth-child(6)::text‘).get() dit[‘风力‘] = wind_power if not dit[‘日期‘] == None: csv_writer.writerow(dit) print(dit) else: print(None)
f = open(‘天气.csv‘, mode=‘a‘, encoding=‘utf-8-sig‘, newline=‘‘) csv_writer = csv.DictWriter(f, fieldnames=[‘日期‘, ‘最高温度‘, ‘最低温度‘, ‘天气‘, ‘风向‘, ‘风力‘]) csv_writer.writeheader() f.close()
运行代码,效果如下图
原文:https://www.cnblogs.com/hhh188764/p/13714169.html