import threading from flask import Flask, render_template, request from crawl.paqu import MultiThreadHotDataSpider, MultiThreadSpecialDataSpider,HKConnectSpider,DataStorage app = Flask(__name__) hot_data = [] special_data = [] hkconnect_data = [] @app.route('/') def index(): return render_template('index.html') @app.route('/hot') def hot(): return render_template('hot.html', hot_data=hot_data) @app.route('/special') def special(): return render_template('special.html', special_data=special_data) @app.route('/hkconnect') def hkconnect(): return render_template('hkconnect.html', hkconnect_data=hkconnect_data) @app.route('/start_spider') def start_spider(): def spider(): global hot_data, special_data, hkconnect_data hot_spider = MultiThreadHotDataSpider() hot_data = hot_spider.get_data() special_spider = MultiThreadSpecialDataSpider() special_data = special_spider.get_data() hkconnect_spider = HKConnectSpider() hkconnect_data = hkconnect_spider.parse(hkconnect_spider.start_urls[0]) data_storage = DataStorage() data_storage.insert_hot_data(hot_data) data_storage.insert_special_data(special_data) data_storage.insert_hkconnect_data(hkconnect_data) t = threading.Thread(target=spider) t.start() return '爬虫已经开始' if __name__ == '__main__': app.run(host="0.0.0.0",debug=True)写出以上代码的前端html实现代码
时间: 2023-07-16 15:16:32 浏览: 91
由于没有给出index.html、hot.html、special.html和hkconnect.html的具体内容,我无法直接提供完整的实现代码。但是可以给你一个参考的思路:
在index.html中添加一个按钮,点击可以触发start_spider路由的访问。例如:
```html
<!DOCTYPE html>
<html>
<head>
<title>爬虫数据展示</title>
</head>
<body>
<h1>爬虫数据展示</h1>
<p>点击下面的按钮开始爬虫</p>
<button onclick="startSpider()">开始爬虫</button>
</body>
<script>
function startSpider() {
var xhr = new XMLHttpRequest();
xhr.open('GET', '/start_spider');
xhr.send();
alert('爬虫已经开始');
}
</script>
</html>
```
在hot.html、special.html和hkconnect.html中分别展示对应的数据。例如:
```html
<!DOCTYPE html>
<html>
<head>
<title>热门数据展示</title>
</head>
<body>
<h1>热门数据展示</h1>
<table>
<thead>
<tr>
<th>标题</th>
<th>链接</th>
<th>阅读量</th>
</tr>
</thead>
<tbody>
{% for item in hot_data %}
<tr>
<td>{{ item.title }}</td>
<td>{{ item.link }}</td>
<td>{{ item.read_count }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
```
其他两个页面的HTML代码类似,只需要将数据展示的表头和tbody里面的数据项替换成对应的数据即可。
阅读全文