from bs4 import BeautifulSoup
import requests
import parsel
import pandas as pd
import numpy as np
weather = [] #创建一个列表用于在for循环中保存每次循环的数据
for year in range(2018,2024): # 设置年份2018-2023年
for month in range(1,13): # 设置月份1-12月
# get/post
url = f'https://tianqi.2345.com/Pc/GetHistory?areaInfo[areaId]=60294&areaInfo[areaType]=2&date[year]={year}&date[month]={month}'
response = requests.get(url=url)
#print(response)
#print(response.json()['data'])
select = parsel.Selector(response.json()['data'])
trs = select.css('table tr')
for tr in trs[1:]:
tds = tr.css('td::text').getall()
weather.append(tds) # 将每次循环时产生的数据存在列表weather中
#print(tds)
print(weather)
爬取结果: