[英]How to edit HTML Website Information from Python String
I'm gonna present you my current situation and then I'll go into detail. 我将向您介绍我目前的情况,然后我将详细介绍。
Currently I have a Python Script which calls an API which retrieves a JSON. 目前,我有一个Python脚本,该脚本调用一个可检索JSON的API。 Inside the python script I already formatted the json string, meaning I have variables which contain strings.
在python脚本中,我已经格式化了json字符串,这意味着我有包含字符串的变量。
Secondly I have an HTML Website with CSS and JS included etc. I already put down a few divs and hr, to design the layout. 其次,我有一个包含CSS和JS等的HTML网站。我已经放下一些div和hr,以设计布局。
THE GOAL: is that once I open the HTML Website, it automatically executes the python script and fills out the areas where I want to put data in. 目标:打开HTML网站后,它会自动执行python脚本并填写我要在其中放置数据的区域。
My Python script sends a request to an API which gives me a JSON back where I specifically want the Value "rocket type" from the JSONObject "rocket": 我的Python脚本向API发送了一个请求,该请求向我提供了JSON,我特别希望从JSONObject“ rocket”获得Value“ rocket type”:
import requests
url = 'https://api.spacexdata.com/v2/launches/latest'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
response = requests.get(url,headers).json()
print("----------------------")
print(response["rocket"]["rocket_type"]) #This is what I meant above with JSONObjects etc...
To give you an idea of how the JSON String looks like : 让您了解JSON字符串的外观:
{
"flight_number": 66,
"mission_name": "Iridium NEXT Mission 7",
"launch_year": "2018",
"launch_date_unix": 1532518766,
"launch_date_utc": "2018-07-25T11:39:26.000Z",
"launch_date_local": "2018-07-25T04:39:26-07:00",
"rocket": { <---- Remember this? My python script wanted to jump into this object via ["rocket"]
"rocket_id": "falcon9",
"rocket_name": "Falcon 9",
"rocket_type": "FT", <---- Remember this? My python script wanted exactly this value via ["rocket_type"]
"first_stage": {
"cores": [
{
"core_serial": "B1048",
"flight": 1,
"block": 5,
"reused": false,
"land_success": true,
"landing_type": "ASDS",
"landing_vehicle": "JRTI"
}
....
My HTML Website is pretty boring, just Divs and other tags so there's no need to add that entirely here. 我的HTML网站非常无聊,只是Divs和其他标签,因此无需在此处完全添加。 But to give you an example of what I want to archive:
但是给我一个我想存档的例子:
This is a div with a paragraph tag for example. 例如,这是一个带有段落标签的div。 I want to tell the HTML to edit the value of the
我想告诉HTML编辑
tag to the response variable from my python script (rocket_type, remember?). 从我的python脚本标记为响应变量(rocket_type,还记得吗?)。
<div class ="boringDiv"><p> EDIT ME TO THE STRING OF A PYTHON VARIABLE! </p></div>
So now we need to replace "EDIT ME TO THE STRING OF A PYTHON VARIABLE" to the value of the python variable "response" which contains the value "FT". 因此,现在我们需要将“编辑我到PYTHON变量的字符串”替换为包含值“ FT”的python变量“ response”的值。
I have no idea where to start (which is why I'm asking), but I would start by making an EXECUTION ORDER: 1. Python; 我不知道从哪里开始(这就是我要问的原因),但是我将从制定执行命令开始:1. Python; 2. HTML....
2. HTML...。
You could use a tool called jinja to populate your HTML in a very clean manner. 您可以使用名为jinja的工具以非常干净的方式填充HTML。 It'd look something like this
看起来像这样
<div class ="boringDiv"><p>{{ rocket_type }}</p></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.