网页抓取(内容是天气数据类的),将数据处理结果保存进入mysql,利用pyecharts处理结果可视化,flask联通前后端,(mysql到pyecharts网页),有首页界面(bootstrap),有登录界面,这个实践项目的步骤及代码
时间: 2024-06-08 07:07:24 浏览: 19
这个实践项目的步骤及代码如下:
1. 网页抓取
使用Python的requests库和BeautifulSoup库进行网页抓取。以下是示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'http://www.weather.com.cn/weather/101010100.shtml'
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'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
today_weather = soup.find('p', {'class': 'wea'}).text
today_temperature = soup.find('p', {'class': 'tem'}).find('span').text
```
2. 将数据处理结果保存进入MySQL
使用Python的pymysql库连接MySQL数据库,并将数据保存到表中。以下是示例代码:
```python
import pymysql
db = pymysql.connect(host='localhost', user='root', password='password', database='weather_db', charset='utf8')
cursor = db.cursor()
sql = f"INSERT INTO weather_data(today_weather, today_temperature) VALUES ('{today_weather}', '{today_temperature}')"
cursor.execute(sql)
db.commit()
db.close()
```
3. 利用pyecharts处理结果可视化
使用Python的pyecharts库进行数据可视化。以下是示例代码:
```python
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = Bar()
bar.add_xaxis(['today_weather', 'today_temperature'])
bar.add_yaxis('weather data', [today_weather, today_temperature])
bar.set_global_opts(title_opts=opts.TitleOpts(title="Weather Data"))
bar.render('weather_data.html')
```
4. 使用Flask联通前后端
使用Python的Flask框架搭建Web应用,将前端页面和后端处理代码连接起来。以下是示例代码:
```python
from flask import Flask, render_template, request, redirect
import pymysql
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if username == 'admin' and password == 'password':
return redirect('/weather')
else:
return redirect('/login')
else:
return render_template('login.html')
@app.route('/weather')
def weather():
db = pymysql.connect(host='localhost', user='root', password='password', database='weather_db', charset='utf8')
cursor = db.cursor()
sql = "SELECT * FROM weather_data"
cursor.execute(sql)
data = cursor.fetchone()
today_weather = data[1]
today_temperature = data[2]
db.close()
return render_template('weather.html', today_weather=today_weather, today_temperature=today_temperature)
if __name__ == '__main__':
app.run(debug=True)
```
5. 首页界面(Bootstrap)
使用HTML和Bootstrap库搭建首页界面。以下是示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Weather Data</title>
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Weather Data</h1>
<p><a href="/login">Login</a></p>
</div>
</body>
</html>
```
6. 登录界面
使用HTML和Bootstrap库搭建登录界面。以下是示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Login</h1>
<form method="POST" action="/login">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" id="username" name="username">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="password" name="password">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</body>
</html>
```
7. MySQL到pyecharts网页
使用Flask框架将pyecharts生成的HTML页面展示在Web应用中。以下是示例代码:
```python
@app.route('/visualization')
def visualization():
return render_template('weather_data.html')
```
这个实践项目的完整代码可以参考以下链接:
https://github.com/zhengyima/Weather-Data-Project
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)