您好,我一般是編碼新手。我想創建一個小專案,以便我可以通過 Flask 和 Python 跟蹤 Web 應用程式中的黃金價格。
html 代碼只顯示一個按鈕,按下它會轉到新路線并在那里顯示黃金價格。我可以通過css檔案毫無問題地設定第一條路線的html代碼。如何設計第二條路線?因為如果我執行所有操作,它只會在瀏覽器的左上角顯示預期的價格。那么如何將一個css檔案連接到這個回傳值呢?
這是我用來查找價格并在瀏覽器中顯示的 Python 代碼。
from flask import Flask, render_template, request
from bs4 import BeautifulSoup
import requests
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/goudprijs")
def priceTracker():
url = 'https://finance.yahoo.com/quote/GC=F?p=GC=F'
page = requests.get(url)
soup = BeautifulSoup(page.text, 'lxml')
price = soup.find_all('div', {'class':'D(ib) Mend(20px)'})[0].find('fin-streamer').text
return(price)
您可以在下面找到我的 html 代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> Gold tracker</title>
<link rel="stylesheet" href="{{url_for('static', filename='css/main.css')}}">
</head>
<body>
<p>GOLD TRACKER</p>
<br>
<form action="goudprijs" method="get">
<p> Whats the price?</p>
<br>
<input type="submit" value="Update" id="buttonprijs">
</form>
</body>
</html>
uj5u.com熱心網友回復:
使用render_template并分配您的可變價格,然后在您的 html 代碼中插入 {{ variable_name }} 在這種情況下 {{ price }}
from flask import Flask, render_template, request
from bs4 import BeautifulSoup
import requests
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/goudprijs")
def priceTracker():
url = 'https://finance.yahoo.com/quote/GC=F?p=GC=F'
page = requests.get(url)
soup = BeautifulSoup(page.text, 'lxml')
price = soup.find_all('div', {'class':'D(ib) Mend(20px)'})[0].find('fin-streamer').text
return render_template('goudprijs.html', price=price)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> Gold tracker</title>
<link rel="stylesheet" href="{{url_for('static', filename='css/main.css')}}">
</head>
<body>
<p>GOLD TRACKER</p>
<br>
<form action="goudprijs" method="get">
<p> Whats the price?</p>
<br>
<p><h4>Price : <h4/> {{ price }}
<input type="submit" value="Update" id="buttonprijs">
</form>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/443953.html
上一篇:自定義ListviewsetonItemClickListner在DialogFragment中使用kotlin中的Viewbinding不起作用
