python - 如何将文本从 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']))
解决方案
最接近的 html 等效于\n
“硬中断”<br/>
标签。
它不需要关闭,因为它不包含任何内容,并且直接表示换行符。
假设 telegra.ph 支持它,您可以简单地:
tmp.replace('\n', '<br/>');
推荐阅读
- java - Spring Boot 应用程序以退出代码 1 终止
- java - Intellij Idea Spring引导开发工具在创建bean时带有liveloader扩展错误
- node.js - 来自不同集合的快速请求第二个参数返回未定义
- nosql - 最能描述 NoSQL 数据库的图表是什么?
- math - 给出奇怪值的基本算术
- python-3.x - 想知道如何在 Mac 上删除所有与 python 相关的文件
- python - ROS图像主题帧率极低
- c++ - 访问指针向量的向量元素
- mysql - MySQL:尝试通过在相同的键名上连接 2 个表来创建表
- python - pandas concat 长度不等的数据帧并设置多索引