使用Python创建HTML

2024-04-18 23:29:51 发布

您现在位置:Python中文网/ 问答频道 /正文

今天我有一个很难和你们讨论的话题,它是关于Python的。 我做了一个python项目,从随机html解析天气预报,现在我想用flask创建一个web框架,这样,我就可以把python脚本转换成html应用程序。你知道吗

但我的问题是我不知道如何在烧瓶中声明我的函数! 我希望有人能帮上忙。你知道吗

这是我的应用程序类型地址:

我想我的问题是在函数def do_something(day,city)中

import urllib, os
import urllib.request
from bs4 import BeautifulSoup

from flask import Flask, request, render_template,jsonify
app = Flask(__name__)

def do_something(day,city):
    if os.path.isfile('weather.txt'):
        os.remove('weather.txt')

    response = urllib.request.urlopen('https://mgm.gov.tr/eng/forecast-some-world-cities.aspx')
    html = response.read()
    html = html.decode('utf-8')

    with open('weather.txt', 'w', encoding='utf-8') as f:
        f.write(html)

    with open('weather.txt', 'r', encoding='utf-8') as f:
        html = f.read()

    soup = BeautifulSoup(html, 'lxml')

    # parsing today's forecast
    todayTemp = []
    for row in soup.select('tbody > tr'):
        temp = row.select('td')
        city = row.select('th')
        todayTemp.append(
            city[0].text + ": Max " + temp[1].text + " °C, " + "Min " + temp[2].text + " °C"
        )
    dateOFtoday = []
    for row in soup.select('thead > tr'):
        date = row.select('th')
        dateOFtoday.append(
            date[1].text
        )

    if day == '1':
        for elem in todayTemp:
            if city.lower() in elem or city.upper() in elem:
                combine = dateOFtoday[0] + elem
                return combine


@app.route('/')
def home():
   return render_template('flaskApp.html')

@app.route('/join', methods=['GET', 'POST'])
def my_form_post():
               day = request.form['day']
               city = request.form['city']
               combine = do_something(day, city)
               result = {
                   "output": combine
               }
               result = {str(key): value for key, value in result.items()}
               return jsonify(result=result)


if __name__ == '__main__':
    app.run(debug=True)

这是我的烧瓶.html地址:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>First Flask App</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<script>
function myFunction() {
var day= $('#day').val();
var city= $('#city').val();
  $.ajax({
              url: "/join",
              type: "POST",
              data: {day:day,
              city:city}
          }).done(function(response) {
            var html= "<br><br><br><p> <b> RESULT : <b><p>";
            response =response.result;
                 $.each(response,function(key,val){
                 console.log(val);
                    html+="<p>"+val+"<p>"
                });
                html +="<br>";
                $(".show-data").append(html);
            });
};
  </script>
<body>
    <p>
        Taking input from web<br><br>
            day<input type="text" id="day" name="day"><br><br>
            city<input type="text" id="city" name="city"><br><br>
            <button id="clicked" onclick="myFunction()">Submit</button>
        </p>
    <div class="show-data" >

    </div>

</body>
</html>

事实上,我没有看到任何错误在这两个代码! web包含2个输入字段(日期和城市):

用户应在“日期”字段中键入“1”作为今天的日期,并在“城市”字段中键入城市名称,最后在按“提交”按钮时

它必须返回两个字符串的总和(日期+城市温度)

但现在当我按下submit时,它不会向用户显示任何内容。你知道吗


Tags: textinbrimportappcityresponserequest