【问题标题】:How can I change inline styling in a local html file from a Python script?如何从 Python 脚本更改本地 html 文件中的内联样式?
【发布时间】:2019-10-30 18:37:38
【问题描述】:

我正在制作一个状态页面。本质上,它将是一个带有一点 CSS 的基本 HTML 页面,显示服务器列表和一个绿色气泡表示在线,红色表示离线。我的页面看起来像我想要的那样,我的脚本可以 ping 所有的服务器工作。我通过使用一个非常简单的跨度标签来实现气泡,然后将背景颜色设置为青柠色。

Python 脚本会遍历服务器列表,并对每个服务器执行四次 ping 操作。之后,它会检查结果并打印一条消息以显示服务器是否在线。

我的问题是我无法让两者一起工作。如何使用 Python 和 BeautifulSoup 进入我的 HTML 文件并根据 ping 的结果修改元素的样式?

示例:服务器 1 在线,圆圈为绿色。服务器 2 出现故障,脚本发现 ping 错误。然后脚本导航到 HTML 并将服务器 2 的跨度背景颜色更改为红色。

这是python脚本的代码:

# import dependencies
import os
import pandas
from bs4 import BeautifulSoup
import re
soup = BeautifulSoup(open('index.html'), 'html.parser')

# import servernames from csv, convert to list
colnames = ['SERVERNAME']
data = pandas.read_csv('Server_List.csv', names=colnames)
hostnames = data.SERVERNAME[1:].tolist()

# ping each server
for hostname in hostnames:
    # pings each server four times
    response = os.system("ping -n 4 " + hostname)
    head, sep, tail = hostname.partition('.')
    # check the response
    if response == 0:
        print('\n', flush=True)
        print(head.upper(), 'is up!\n', flush=True)
    else:
        print('\n', flush=True)
        print(head.upper(), 'is down!\n', flush=True)

这是我的跨度之一:

<span class="status" style="background-color: limegreen;">

这是我的课:

.status {
    padding: 2px 11px;
    border-radius: 100%;
}

【问题讨论】:

  • 您可以在每次使用 HTML 模板进行更新时重新生成整个 html,例如 this;或查看herehere 了解更多信息。 BeautifulSoup 通常用于从 HTML 中解析和提取信息,而不是对其进行编辑。
  • @xarantolus 该解决方案的唯一问题是我需要单独修改每个服务器。使用模板,我需要为离线和在线的每种可能组合使用一个模板,不是吗?我也应该在帖子中声明,如果需要的话,我绝不反对放弃 BS4 换取其他图书馆。
  • 您不需要所有可能的组合,但您需要检查每个服务器的状态(例如,使用ifelse,就像您已经在做的那样)。除了打印状态,您还可以将正确的 html(取决于您的条件)附加到字符串中,然后将其放入文件中
  • 我对你的追加完全没问题,只是不知道如何将它从脚本移动到 html。有没有一种简单的方法可以做到这一点,同时保持它与每个单独的服务器相关联?
  • 您可以将替换字符串放入您的html文件中,例如$$replace_this$$。然后你的脚本在开头将index.html 文件读入变量template。您还可以在开头定义一个字符串,将您的 html 附加到该字符串,该字符串将放置在$$replace_this$$ 的位置。这个 html 被附加到每个服务器上,也就是在区分结果之后在 for hostname in hostnames: 中,然后最后你在你的 template 字符串上调用 replace 并存储它的结果。那就是你想要的html。

标签: python html


【解决方案1】:

您需要动态生成 HTML。

这可以使用模板引擎完成,但您也可以“手动”完成。

这是一个关于它如何工作的例子:

import os
import pandas
import re

import html

# Define the template string. Could also be loaded from a file
template = """<html>
<head>
</head>
<body>
Server Status:

$$replace_this$$

</body>
</html>"""

# import servernames from csv, convert to list
colnames = ['SERVERNAME']
data = pandas.read_csv('Server_List.csv', names=colnames)
hostnames = data.SERVERNAME[1:].tolist()


html_input = ""

# ping each server
for hostname in hostnames:
    # ping server four times
    response = os.system("ping -n 4 " + hostname)
    head, sep, tail = hostname.partition('.')

    # Append server name, escape server name to prevent injection attacks (in case data contains malicious content)
    html_input += "<p>" + html.escape(hostname)

    # check the response
    if response == 0:
        html_input += "Server is <b>up</b>"
    else:
        html_input += "Server is <b>down</b>"

    html_input += "</p>\n"

html_result = template.replace("$$replace_this$$", html_input)

# Now you can use html_result for anything, such as writing it to a file.

请注意,这不是最有效或最专业的方法,但它可能效果很好。

【讨论】:

  • 我做了一些细微的改动,让它与我在这里的实际设置一起工作,效果很好。非常感谢您的帮助!将其标记为肯定的答案!
  • 如何为多个变量替换这个?
猜你喜欢
  • 2015-06-24
  • 2010-12-26
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
  • 2015-01-03
  • 1970-01-01
  • 2012-06-26
  • 2013-05-27
相关资源
最近更新 更多