Python爬取天气预报,Ta的城市开始降温了

基本环境配置

python 3.6

pycharm

requests

parsel

csv

相关模块pip安装即可

确定目标网页

分析网页

打开开发者工具常规操作~

看一下网页返回的数据,是否有自己有要的~

如图所示,这个网站是静态网页,数据都在网页中的 tr 标签里面

代码部分

请求网页数据

import requests

url = 'https://weather.mipang.com/changsha/9yuefen.html'

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 print(dit)



保存数据

f = open('天气.csv', mode='a', encoding='utf-8-sig', newline='')
csv_writer = csv.DictWriter(f, fieldnames=['日期', '最高温度', '最低温度', '天气', '风向', '风力'])
csv_writer.writeheader()
csv_writer.writerow(dit)f.close()



关注我,了解更多知识

你可能感兴趣的:(Python爬取天气预报,Ta的城市开始降温了)