我正在尝试构建一个小型Web应用程序。一个简单的价格搜寻器。我试图将其组合成一个Web应用程序,但没有成功。
这是我拥有的python代码的工作片段,返回的特定项目的价格为:
918193-012
基本上的想法是:用户输入产品的股票代码,作为回报,在同一页或另一页上,他将收到该产品的价格。任何建议表示赞赏。
import requests
from bs4 import BeautifulSoup
import subprocess
url = f'https://www.tennis.fr/catalogsearch/result/?q=918193-012'
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
capturedPrice = soup.findAll('p', class_="special-price")[0].text
capturedPrice = capturedPrice.strip('0')
capturedPrice = capturedPrice.strip()
print(capturedPrice[35:])
这是我的app.py
from flask import Flask, jsonify, render_template, request
from bs4 import BeautifulSoup
import subprocess
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
这里是:index.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Mono&display=swap" >
<style>
body {
font-family: 'Roboto Mono', monospace;
}
* {
box-sizing: border-box;
}
form.example input[type=text] {
padding: 10px;
font-size: 17px;
border: 1px solid grey;
float: left;
width: 80%;
background: #f1f1f1;
}
form.example button {
float: left;
width: 20%;
padding: 10px;
background: #2196F3;
color: white;
font-size: 17px;
border: 1px solid grey;
border-left: none;
cursor: pointer;
}
form.example button:hover {
background: #0b7dda;
}
form.example::after {
content: "";
clear: both;
display: table;
}
</style>
</head>
<body>
<center>
<h1>Find the best prices</h1>
<p><h4>Stock code to test: 918193-012</h3></p>
<form class="example" action="" style="margin:auto;max-width:500px">
<input type="text" placeholder="Stock code" name="">
<button type="submit"><i class="fa fa-search"></i></button>
</form>
</center>
</body>
</html>
答案 0 :(得分:1)
您要从HTML站点中提取输入信息。一种可能的解决方案是使用以下形式:
app.py:
from flask import Flask
from flask_wtf import FlaskForm
from flask import render_template
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
app = Flask(__name__)
class InputForm(FlaskForm):
input = StringField('Input',validators=[DataRequired()]
submit = SubmitField('Search for Price')
@app.route('/')
def index():
form = InputForm()
if form.validate_on_submit():
result = form.input.data
return render_template("index.html", result=result, form=form)
else:
return render_template("index.html",form=form)
if __name__=="__main__":
app.run(debug=True)
如果需要,可以将StringField更改为IntegerField,就这样。 下一步是将sth添加到您的@ app.route:
当然,您需要向HTML站点添加一些代码,以将搜索值传递给Web应用。
您需要通过以下表单来代替普通的html按钮:
<!DOCTYPE html>
<html>
<head>
##your css stuff##
<style>
</style>
</head>
<body>
<center>
<h1>Find the best prices</h1>
<p><h4>Stock code to test: 918193-012</h3></p>
{{ form.input(class="form-control") }}
{{ form.submit(class="btn") }}
{% if result == #yourvalue# %}
#your printed data#
{% else %}
{% endif %}
</center>
</body>
</html>
在此示例中,引导程序用于设计,但是那可以改变您想要的方式 希望我的文字可以理解
编辑1:
如果只想使用一个html文件,则可以在html站点内使用if语句。在我的示例中,您将结果通过render_template行传递给html。