问题描述
我一直在尝试使用 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
如果有人能提供帮助,将不胜感激!谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)