首页 > 解决方案 > 为什么python模块requests_html仅在从输出中的字符串导入后取消粗体和下划线效果?

问题描述

所以我有这个代码。

只是一些红色、粗体、下划线和 endc 的 ansi 转义码。该代码正在屏幕上打印修改后的字符串。

bold = "\033[1m"
underlined = "\033[4m"
red = "\033[91m"
endc = "\033[0m"

def red_bold_underlined(string):
    return bold + underlined + red + string + endc

x = red_bold_underlined('stackoverflow question')
print(x)
print(x.__repr__())

# pip install requests_html
from requests_html import HTMLSession

print()
print(x)
print(x.__repr__())

代码输出 https://i.stack.imgur.com/KMNh5.jpg

如果我评论该行: from requests_html import HTMLSession

带有注释行的输出 https://i.stack.imgur.com/fjMtt.jpg

此外,在这些图像中,每个输出下都是字符串的表示版本,并且在每种情况下都是相同的,但问题是我不明白 requests_html 里面有什么,因为它在导入后取消了粗体下划线效果。

我也尝试了其他模块,但这仅在我导入 requests_html 或从 requests_html 导入某些内容时才会发生。

我的问题是:我如何修复代码或问题以便在终端上输出粗体和下划线效果并在代码中导入 requests_html 模块?

标签: pythoneffectunderlinepython-requests-html

解决方案


推荐阅读