在使用html2text库的时候,如果表格中有p标签,输出的表格格式不对,虽然有人在gitud中提出过此类问题,但我没有找到作者的解决方法,现在有什么办法?
我现在想到的就是利用正则,删除p还有strong。
类似html如下
<td style="width:6.92%;border:solid black 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:30px;">
<p align="center" style="text-align:center;line-height:150%;layout-grid-mode:char;"><strong>序号</p>
</td>
变成
<td style="width:6.92%;border:solid black 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:30px;">
序号
</td>
我写的是
content = re.sub(r'(<p>)(<strong>)', r'', html)
content = re.sub(r'(</strong>)(</p>)', r'', content)
content = re.sub(r'(<td.*?>)(<p>)', r'', content)
content = re.sub(r'(</p>)(</td>)', r'', content)
但是删除不了,是因为换行符之类的原因吗,应该怎么办呢
content = re.sub(r"(<p.*?)(?=序号)", r'', content, re.DOTALL)
content = re.sub(r"(?<=序号)(.*?</p>)", r'', content, re.DOTALL)
import re
html = """<td style="width:6.92%;border:solid black 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:30px;">
<p align="center" style="text-align:center;line-height:150%;layout-grid-mode:char;"><strong>序号</p>
</td>
"""
content = re.sub(r'(<p.*<strong>)', r'', html)
content = re.sub(r'</p>', r'', content)
print(content)
结果:
如果觉得答案对你有帮助,请点击下采纳,谢谢~