咱们先看爬取的数据

话不多说
直接开始代码的编写
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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
全部源码放在这
'''目标:爬取天气后报网山东各个城市天气'''
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)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83