【发布时间】:2018-08-01 22:21:39
【问题描述】:
我想从字符串中删除所有 HTML 内容。
我有一个字符串
str= "I am happy with <body> <h1>This is a Heading</h1> <p>This is a paragraph.</p> </body> 3333 <body> <h1>This is a Heading</h1> <p>This is a paragraph.</p> </body> your code"
我想要最后的字符串
str= "I am happy with 3333 your code"
我已经编写了这段代码来完成上述任务。
def removetags(input_str):
result = ''
startflag = 0
start=True
count=0
for ch in input_str:
if ch == '<':
if count!=len(input_str)-1:
if input_str[count+1]!='/':
start=True
startflag += 1
elif (ch == '>') and startflag :
if not start:
startflag -= 1
start=False
elif (not startflag) :
result += ch
count += 1
return result
print(removetags(str))
这可以正常工作,但如果您在文本中有<,那么它将无法正确输出。所以我想使用 html 解析来删除。有没有办法做到这一点?我找到了这个库,但我找不到这样做的方法。提前致谢。
【问题讨论】:
标签: python html parsing jira preprocessor