使用python代码,数据采集,做一个比价系统,有前端后端,前端提交查询的商品,后端启用爬虫代码使用多线程,开始比价,最后返回最低价格,该商品的链接,图片等
时间: 2024-04-21 14:26:49 浏览: 120
好的,这个系统需要用到 Python 的 requests 库来获取网页源代码,用到 BeautifulSoup 库来解析网页,用到 threading 库来实现多线程,用到 Flask 库来实现前端和后端的交互。
以下是代码实现:
后端代码:
```python
import requests
from bs4 import BeautifulSoup
from flask import Flask, request, jsonify
import threading
app = Flask(__name__)
def get_price(url):
try:
# 获取网页源代码
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.36 Edge/16.16299'
}
r = requests.get(url, headers=headers)
r.raise_for_status()
r.encoding = r.apparent_encoding
soup = BeautifulSoup(r.text, 'html.parser')
# 解析网页获取商品价格和图片链接
price = soup.find('span', {'class': 'price'}).get_text()
img_url = soup.find('img', {'class': 'img'}).get('src')
return price, img_url
except:
return '获取价格失败', ''
@app.route('/compare', methods=['POST'])
def compare():
# 获取前端提交的商品名
data = request.json
product_name = data['product_name']
# 获取商品链接列表
url_list = []
url_list.append('https://jd.com/search?keyword=' + product_name)
url_list.append('https://s.taobao.com/search?q=' + product_name)
url_list.append('https://www.amazon.com/s?k=' + product_name)
# 多线程获取商品价格和图片链接
results = []
threads = []
for url in url_list:
t = threading.Thread(target=results.append(get_price(url)), args=(url,))
threads.append(t)
t.start()
for t in threads:
t.join()
# 返回最低价格、商品链接和图片链接
min_price = min(result[0] for result in results)
min_result = [result for result in results if result[0] == min_price][0]
min_url = url_list[results.index(min_result)]
min_img_url = min_result[1]
# 返回结果
result = {
'product_name': product_name,
'min_price': min_price,
'min_url': min_url,
'min_img_url': min_img_url
}
return jsonify(result)
if __name__ == '__main__':
app.run(debug=True)
```
前端代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>比价系统</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<h1>比价系统</h1>
<div>
<label>商品名:</label>
<input id="product_name" type="text">
<button id="compare_btn">比价</button>
</div>
<div id="result">
</div>
<script>
$('#compare_btn').click(function() {
var product_name = $('#product_name').val();
$.ajax({
url: '/compare',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({product_name: product_name}),
success: function(result) {
var html = '<p>商品名:' + result.product_name + '</p>';
html += '<p>最低价格:' + result.min_price + '</p>';
html += '<p>商品链接:' + result.min_url + '</p>';
html += '<p>图片链接:<img src="' + result.min_img_url + '"></p>';
$('#result').html(html);
}
});
});
</script>
</body>
</html>
```
在命令行中运行后端代码:
```
python backend.py
```
然后在浏览器中打开前端页面即可。
阅读全文