python - 如何将数据从 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
如果有人可以提供帮助,将不胜感激!谢谢!
解决方案
推荐阅读
- c# - 为什么这个 Fixture 不止一次运行它的构造函数?
- loops - 使用 Python3,我想减少 if-elif 代码
- react-admin - update 和 updateMany previousData 参数的 react-admin 文档
- python - 数据流中的 ValueError:无效的 GCS 位置:无
- java - JBoss EAP 7.2 EJB 服务器到客户端通信,无需 http
- xml - 使用 XML::Simple 将 XML 解析为散列时如何保留前导空格
- c# - 实体配置问题 INSERT 语句与 FOREIGN KEY 约束冲突
- c# - 如何使 appsettings.json 配置在项目而不是 bin 文件夹中?
- dataframe - 如何为 DataFrame 中的每一行返回一个 DataFrame 并将结果连接到一个 DataFrame 中?
- sql - SQL:跨两列的唯一值