python爬取上海市前十天的最高和最低气温,并存入数据库,同时用matplotlib 实现数据绘图
时间: 2024-03-11 17:43:45 浏览: 20
好的,以下是一个示例代码,可以爬取上海市前十天的最高和最低气温,并将数据存入SQLite数据库,最后使用matplotlib库绘制折线图:
```python
import requests
import sqlite3
import matplotlib.pyplot as plt
from bs4 import BeautifulSoup
# 请求头,模拟浏览器访问
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
# 目标网址
url = 'http://www.tianqihoubao.com/lishi/shanghai/month/202107.html'
# 发送HTTP请求
res = requests.get(url, headers=headers)
# 解析HTML页面
soup = BeautifulSoup(res.text, 'html.parser')
# 获取天气数据表格
table = soup.find('table', {'class': 'b'})
# 连接数据库
conn = sqlite3.connect('weather.db')
cursor = conn.cursor()
# 创建表格
cursor.execute('CREATE TABLE IF NOT EXISTS weather (date TEXT, high_temperature INT, low_temperature INT)')
# 解析表格数据
rows = table.find_all('tr')
for row in rows[1:11]:
data = row.find_all('td')
date = data[0].text.strip()
high_temperature = int(data[1].text.strip().replace('℃', ''))
low_temperature = int(data[2].text.strip().replace('℃', ''))
print(date, high_temperature, low_temperature)
# 将数据插入数据库
cursor.execute('INSERT INTO weather (date, high_temperature, low_temperature) VALUES (?, ?, ?)',
(date, high_temperature, low_temperature))
# 提交事务并关闭连接
conn.commit()
conn.close()
# 从数据库中读取数据
conn = sqlite3.connect('weather.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM weather')
data = cursor.fetchall()
conn.close()
# 绘制折线图
dates = [row[0] for row in data]
high_temperatures = [row[1] for row in data]
low_temperatures = [row[2] for row in data]
plt.plot(dates, high_temperatures, label='High Temperature')
plt.plot(dates, low_temperatures, label='Low Temperature')
plt.xticks(rotation=45)
plt.xlabel('Date')
plt.ylabel('Temperature (℃)')
plt.title('Shanghai Weather')
plt.legend()
plt.show()
```
这段代码会爬取上海市2021年7月份前十天的最高和最低气温,并将数据存入名为`weather.db`的SQLite数据库中。最后使用matplotlib库绘制折线图,展示这些数据。你可以根据需要修改日期、城市等参数。