咱们先看爬取的数据
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e01e459ab66029b1a49c48075c39112e.png)
话不多说
直接开始代码的编写
首先导入第三方库
import requests
import json
import parsel
import csv
打开csv文件写入我们要爬取的数据表头
with open('山东各个城市历史天气.csv', mode='a', newline='', encoding='utf-8')as f:
csv_writer = csv.writer(f)
csv_writer.writerow(['日期','最高温','最低温','天气','风力风向','空气质量指数','城市'])
确定爬取的网站,然后拿到山东所有城市的ID
遍历爬取所有城市的天气
最后保存到本地
city_list=[54823,54734,54714,54736,54906,54915,54806,54938,54857,54945,54827,54843,54774,54765,54830,58024]
for city in city_list:
for year in range(2021,2023):
for month in range(1,13):
url=f'https://tianqi.2345.com/Pc/GetHistory?areaInfo%5BareaId%5D={city}&areaInfo%5BareaType%5D=2&date%5Byear%5D={year}&date%5Bmonth%5D={month}'
response = requests.get(url = url)
json_data = response.json()
html_data= json_data['data']
selector=parsel.Selector(html_data)
trs = selector.css('.history-table tr')[1:]
for tr in trs:
td = tr.css( 'td::text')
if city==54823:
td.append('济南')
elif city==54734:
td.append('滨州')
elif city==54714:
td.append('德州')
elif city==54736:
td.append('东营')
elif city==54906:
td.append('菏泽')
elif city==54915:
td.append('济宁')
elif city==54806:
td.append('聊城')
elif city==54938:
td.append('临沂')
elif city==54857:
td.append('青岛')
elif city==54945:
td.append('日照')
elif city==54827:
td.append('泰安')
elif city==54843:
td.append('潍坊')
elif city==54774:
td.append('威海')
elif city==54765:
td.append('烟台')
elif city==54830:
td.append('淄博')
elif city==58024:
td.append('枣庄')
print(td)
with open('山东各个城市历史天气.csv',mode='a',newline='',encoding='utf-8')as f:
csv_writer=csv.writer(f)
csv_writer.writerow(td)
全部源码放在这
'''目标:爬取天气后报网山东各个城市天气'''
import requests
import json
import parsel
import csv
with open('山东各个城市历史天气.csv', mode='a', newline='', encoding='utf-8')as f:
csv_writer = csv.writer(f)
csv_writer.writerow(['日期','最高温','最低温','天气','风力风向','空气质量指数','城市'])
city_list=[54823,54734,54714,54736,54906,54915,54806,54938,54857,54945,54827,54843,54774,54765,54830,58024]
for city in city_list:
for year in range(2021,2023):
for month in range(1,13):
url=f'https://tianqi.2345.com/Pc/GetHistory?areaInfo%5BareaId%5D={city}&areaInfo%5BareaType%5D=2&date%5Byear%5D={year}&date%5Bmonth%5D={month}'
response = requests.get(url = url)
json_data = response.json()
html_data= json_data['data']
selector=parsel.Selector(html_data)
trs = selector.css('.history-table tr')[1:]
for tr in trs:
td = tr.css( 'td::text')
if city==54823:
td.append('济南')
elif city==54734:
td.append('滨州')
elif city==54714:
td.append('德州')
elif city==54736:
td.append('东营')
elif city==54906:
td.append('菏泽')
elif city==54915:
td.append('济宁')
elif city==54806:
td.append('聊城')
elif city==54938:
td.append('临沂')
elif city==54857:
td.append('青岛')
elif city==54945:
td.append('日照')
elif city==54827:
td.append('泰安')
elif city==54843:
td.append('潍坊')
elif city==54774:
td.append('威海')
elif city==54765:
td.append('烟台')
elif city==54830:
td.append('淄博')
elif city==58024:
td.append('枣庄')
print(td)
with open('山东各个城市历史天气.csv',mode='a',newline='',encoding='utf-8')as f:
csv_writer=csv.writer(f)
csv_writer.writerow(td)