首页 > 解决方案 > 如何将数据从 Python 文件传输到 HTML 文件?

问题描述

我一直在尝试使用 Python 从 API 获取数据并将其显示到 HTML 页面中,但到目前为止我尝试的所有方法似乎都不起作用!我曾尝试使用 Flask 显示 Python 变量,但服务器似乎无法正常工作!这是我的 Python 代码:

ScratchDBtest.py

#imports all the required assets
import requests
import json
import sys
from time import sleep



#this fetches data from API
response = requests.get("https://scratchdb.lefty.one/v2/user/info/" + input("Enter Username: "))
#organises data
def jprint(object):
    #create a formatted string
    text = json.dumps(object, sort_keys = True, indent = 5)
    print(text)

#print(response.status_code)






answer = response.json()

#jprint(answer)

query_time = response.json()['query_time']
if not query_time < 10:
    followers = response.json()['followers']
    status = response.json()['status']
    username = response.json()['username']
    bio = response.json()['bio']
    country = response.json()['country']
    following = response.json()['following']
    result = username + " has " + str(followers) + " followers and has a scratcher status of " + status + ". The user lives in " + country
else:
    result = "An error occured. This account is not real or hasn't been logged into the database yet!"

#typewriting effect
for characters in result:
    sleep(0.01)
    sys.stdout.write(characters)
    sys.stdout.flush()

在这种情况下,我希望“结果”变量显示在 HTML 文件中。我还没有任何 HTML 代码,但文件名为 ScratchDBtest.html

如果有人可以提供帮助,将不胜感激!谢谢!

标签: pythonhtmlapidata-transfer

解决方案


推荐阅读