首页 > 解决方案 > 如何将文本从 shell 转换为 html?

问题描述

可能这是一个非常简单和愚蠢的问题,但我是 Python 新手,所以请处理它

我需要做的 - 在 shell 中执行命令并将其输出为 telegra.ph 页面

问题 - telegra.ph API 在一行中忽略 \n 事物和所有 outpot 文本

使用 Python Telegraph API 包装器 - https://github.com/python273/telegraph

我知道它需要将我的文本转换为 html-like 并删除 <p> 标签,我尝试了一些脚本,但我的程序给了我错误:

telegraph.exceptions.NotAllowedTag: span tag is not allowed

所以,我已经删除了所有跨度标签并得到了相同的结果,就好像我没有转换

然后我尝试使用replace("\n", "<p>")但卡在结束标签中......

代码:

import subprocess

from telegraph import Telegraph

telegraph = Telegraph()
telegraph.create_account(short_name='1111')

tmp = subprocess.run("arp", capture_output=True, text=True, shell=True).stdout

print( '\n\n\n'+tmp+'\n\n\n\n')   ### debug line


response = telegraph.create_page(
    'Random',
    html_content= '<p>' + tmp + '</p>'
)

print('https://telegra.ph/{}'.format(response['path']))

标签: pythonlinux

解决方案


最接近的 html 等效于\n硬中断”<br/>标签
它不需要关闭,因为它不包含任何内容,并且直接表示换行符。

假设 telegra.ph 支持它,您可以简单地:

tmp.replace('\n', '<br/>');

推荐阅读