网页抓取(内容自选),将数据处理结果保存进入mysql,利用pyecharts处理结果可视化,flask联通前后端,(mysql到pyecharts网页),有首页界面(bootstrap),有登录界面,这个实践项目的步骤及代码
时间: 2024-06-08 16:07:26 浏览: 182
django4.0+pyecharts+mysql实现的搜索关键词,展示相关分析数据结果web系统
5星 · 资源好评率100%
步骤:
1. 确定需要抓取的网页和数据字段
2. 使用requests和BeautifulSoup库进行网页抓取和数据解析
3. 使用pymysql库连接MySQL数据库,并创建存储数据的表格
4. 将抓取到的数据插入到MySQL数据库中
5. 使用pyecharts库进行数据可视化,并生成html文件
6. 使用Flask框架建立Web应用,将pyecharts生成的html文件渲染到前端页面
7. 使用Bootstrap库设计前端页面,包括首页和登录界面
8. 添加用户认证功能,使用Flask-Login库实现
9. 部署项目到服务器上,使用Nginx和Gunicorn实现Web服务器和应用服务器的部署
代码:
1. 网页抓取和数据解析
```python
import requests
from bs4 import BeautifulSoup
url = 'http://www.example.com'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
data = soup.find('div', {'class': 'data'}).text.strip()
```
2. 连接MySQL数据库,并创建表格
```python
import pymysql
conn = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='password',
db='database'
)
cursor = conn.cursor()
sql = '''
CREATE TABLE IF NOT EXISTS data (
id INT AUTO_INCREMENT PRIMARY KEY,
data VARCHAR(255)
);
'''
cursor.execute(sql)
conn.close()
```
3. 将数据插入到MySQL数据库中
```python
import pymysql
conn = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='password',
db='database'
)
cursor = conn.cursor()
data = 'example data'
sql = '''
INSERT INTO data (data) VALUES (%s);
'''
cursor.execute(sql, (data,))
conn.commit()
conn.close()
```
4. 使用pyecharts库进行数据可视化,并生成html文件
```python
from pyecharts.charts import Bar
bar = Bar()
bar.add_xaxis(['A', 'B', 'C', 'D', 'E'])
bar.add_yaxis('data', [1, 2, 3, 4, 5])
bar.render('bar.html')
```
5. 使用Flask框架建立Web应用,将pyecharts生成的html文件渲染到前端页面
```python
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/data')
def data():
return render_template('bar.html')
if __name__ == '__main__':
app.run()
```
6. 使用Bootstrap库设计前端页面,包括首页和登录界面
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Homepage</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="/">Homepage</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="/data">Data</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-4">
<h1>Welcome to Homepage!</h1>
<p class="lead">This is a simple example of Flask web application.</p>
</div>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</body>
</html>
```
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body class="bg-light">
<div class="container mt-4">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-header">Login</div>
<div class="card-body">
<form method="POST">
<div class="form-group">
<label>Username</label>
<input type="text" name="username" class="form-control" required>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary btn-block">Login</button>
</form>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</body>
</html>
```
7. 添加用户认证功能,使用Flask-Login库实现
```python
from flask_login import UserMixin
class User(UserMixin):
def __init__(self, id):
self.id = id
def __repr__(self):
return f'<User: {self.id}>'
users = [User(id) for id in range(1, 4)]
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
user = next((user for user in users if user.id == int(username)), None)
if user:
login_user(user)
return redirect('/')
return render_template('login.html')
```
8. 部署项目到服务器上,使用Nginx和Gunicorn实现Web服务器和应用服务器的部署
```bash
sudo apt-get update
sudo apt-get install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo apt-get install python3-pip python3-dev build-essential libssl-dev libffi-dev
sudo -H pip3 install --upgrade pip
sudo -H pip3 install virtualenv
mkdir ~/myproject
cd ~/myproject
virtualenv myprojectenv
source myprojectenv/bin/activate
pip install gunicorn flask flask_login pymysql pyecharts
nano ~/myproject/myproject.py
```
将代码复制到myproject.py文件中,并保存。
```bash
gunicorn --bind 0.0.0.0:8000 myproject:app
```
在浏览器中访问服务器的IP地址和端口号,即可查看部署后的Web应用。
阅读全文