如何将我的 Python 脚本与我的 HTML 文件连接起来?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29734208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:56:47  来源:igfitidea点击:

How can I connect my python script with my HTML file?

pythonhtmldommako

提问by bkhmsi

Basically I am getting some data from a webpage, and putting it into an array, I want to output the contents of that array into a table in a HTML file. After some research I found that using a mako template might be the best solution, but I don't understand how to use it? Can any one guide me through the steps or offer a better solution to execute this python script and output its result on the web?

基本上我从网页中获取一些数据,并将其放入一个数组中,我想将该数组的内容输出到 HTML 文件中的表格中。经过一番研究,我发现使用 mako 模板可能是最好的解决方案,但我不明白如何使用它?任何人都可以指导我完成这些步骤或提供更好的解决方案来执行此 python 脚本并将其结果输出到网络上吗?

import urllib2
import mako
from bs4 import BeautifulSoup as BS

html = urllib2.urlopen("<link-to-web-page>")
soup = BS(html)
data = []


for each_course in soup.findAll('li',{'class':'<class-name>'}):
    inner_text = each_course.text
    data.append(inner_text)


for i in data:
    print (i+"\n")

采纳答案by syntonym

There are two subproblems in your problem:

你的问题有两个子问题:

  • Generate HTML to show your data
  • Serve that HTML
  • 生成 HTML 以显示您的数据
  • 提供该 HTML

Mako can help you with the first one. For the second one there are different solutions available that depend on your situation.

Mako 可以帮助您解决第一个问题。对于第二个,根据您的情况有不同的解决方案可用。

Generate HTML

生成 HTML

First you have to decide on a template, that means on the general skeleton in which your data will then be filled in. If you only want to show your data without any further information enigmas answer will work, but if it gets more complicated it is useful to use something as mako. How does a general template look like? Here is a really simple one:

首先你必须决定一个模板,这意味着你的数据将被填写在一般框架上。如果你只想显示你的数据而没有任何进一步的信息,那么谜题答案会起作用,但如果它变得更复杂,那就是使用某些东西作为 mako 很有用。通用模板是什么样子的?这是一个非常简单的:

<html>
<body>
Hello world!
</body>
</html>

This doesn't do very much. It's just like a single string. So lets get some python in it:

这并没有多大作用。它就像一个单一的字符串。所以让我们在其中加入一些python:

<html>
<body>
${x}
</body>
</html>

This template contains a variable which you will need to provide:

此模板包含您需要提供的变量:

template = Template(filename="yourtemplate.template") # or how ever you named your template
print(template.render(x="Hello World!")

You will at least need for loops:

您至少需要 for 循环:

% for a in [1,2,3]
${a}
% endfor

This is the basic syntax for a loop. Of course you can do more complex things. Imagine mylist is a list of Personinstances with a name and a age:

这是循环的基本语法。当然你可以做更复杂的事情。想象一下 mylist 是一个Person带有名称和年龄的实例列表:

% for person in mylist
Name: ${person.name}
Age: ${person.age}
% endfor

You can use arbitrary HTML inside of that. Ofcourse mako can do more powerfull things, but a single stackoverflow post is to little space for that. You can read the basic usageand/or Syntaxpage of the mako language for mor information. But with the here presented structures you should be able to finish your task.

您可以在其中使用任意 HTML。当然 mako 可以做更强大的事情,但是单个 stackoverflow 帖子的空间很小。您可以阅读mako 语言的基本用法和/或语法页面以获取更多信息。但是使用这里介绍的结构,您应该能够完成您的任务。

Serve HTML

提供 HTML

You still need to somehow bring the HTML out to the web or where ever you want it. You have multiple possibilities that depend on what you want:

您仍然需要以某种方式将 HTML 发布到网络或您想要的任何地方。您有多种可能性,这取决于您想要什么:

Static or dynamic

静态或动态

  • Is your data static? That means, will your data change in near time? If no, than you can simply generate the HTML on your local computer and then push the html to a simple webserver that serves html.

  • Is your data dynamic? That means your data changes often and it is not reasonable to powerup your local machine, run your script and then push the HTML. Instead you have to tell the server that serves your webpage to run your script whenever the data changes. There are more then one possibility to do that, you can use CGI (a webserver like nginx or apache calls your python script and serves the output) or a wsgi framework like djangoor flaskor others. Of course these also need to be served, either from a "typical" webserver like apache or nginx or something like gunicorn

  • 你的数据是静态的吗?这意味着,您的数据会在近期发生变化吗?如果不是,那么您可以简单地在本地计算机上生成 HTML,然后将 html 推送到提供 html 的简单网络服务器。

  • 你的数据是动态的吗?这意味着您的数据经常更改,启动本地机器、运行脚本然后推送 HTML 是不合理的。相反,您必须告诉为您的网页提供服务的服务器在数据发生变化时运行您的脚本。这样做的可能性不止一种,您可以使用 CGI(像 nginx 或 apache 这样的网络服务器调用您的 python 脚本并提供输出)或像djangoflask或其他这样的 wsgi 框架。当然,这些也需要提供服务,要么来自像 apache 或 nginx 这样的“典型”网络服务器,要么像gunicorn

Lan or WWW?

局域网还是万维网?

  • If you only need it to be available in the LAN you can simply run a webserver on your local computer. If you do not expect much traffic and security is not a concern you could use the http server in the python standard library.

  • If you need it to be available on the web you need to look for a webserver. There are a few services that are free of charge for low traffic. To name a few: herokuwhich has a focus on python, so it's suited for the dynamic use case. Github pageswhere you can directly serve HTML from a github repository. I think it can only serve static HTML.

  • 如果您只需要它在 LAN 中可用,您只需在本地计算机上运行网络服务器即可。如果您不希望有太多流量并且安全性不是问题,您可以使用python 标准库中http 服务器

  • 如果您需要它在网络上可用,则需要寻找网络服务器。有一些服务对于低流量是免费的。仅举几例:heroku专注于 Python,因此适用于动态用例。Github 页面,您可以在其中直接从 github 存储库提供 HTML。我认为它只能提供静态 HTML。

回答by enigma

data = [1, 2, 3, 4]


def data_to_html_table(data):
    html = '<table><tbody>'
    for item in data:
        html += '<tr><td>' + str(item) + '</td></tr>'
    html += '</tbody></table>'
    return html

print data_to_html_table(data)

results in html equivalent to

结果相当于 html

<table>
    <tbody>
        <tr>
            <td>1</td>
        </tr>
        <tr>
            <td>2</td>
        </tr>
        <tr>
            <td>3</td>
        </tr>
        <tr>
            <td>4</td>
        </tr>
    </tbody>
</table>