我需要处理一个非常混乱的HTML归档文件,其中包含大量不必要的表格、跨度和内联样式,并将其转换为Markdown。
我尝试使用Beautiful Soup来完成这个任务,我的目标基本上是get_text()
函数的输出,除了保留带有完整href
的锚标签。
例如,我想要将以下内容转换为Markdown:
<td>
<font><span>Hello</span><span>World</span></font><br>
<span>Foo Bar <span>Baz</span></span><br>
<span>Example Link: <a href="https://google.com" target="_blank" style="mso-line-height-rule: exactly;-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;color: #395c99;font-weight: normal;text-decoration: underline;">Google</a></span>
</td>
Into:
Hello World
Foo Bar Baz
Example Link: <a href="https://google.com">Google</a>
目前我的思路是获取所有标记,并在它们不是锚点时将它们全部解包,但这会导致文本重复多次,因为soup.find_all(True)
递归地返回嵌套的标记作为单独的元素:
#!/usr/bin/env python
from bs4 import BeautifulSoup
example_html = '<td><font><span>Hello</span><span>World</span></font><br><span>Foo Bar <span>Baz</span></span><br><span>Example Link: <a href="https://google.com" target="_blank" style="mso-line-height-rule: exactly;-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;color: #395c99;font-weight: normal;text-decoration: underline;">Google</a></span></td>'
soup = BeautifulSoup(example_html, 'lxml')
tags = soup.find_all(True)
for tag in tags:
if (tag.name == 'a'):
print("<a href='{}'>{}</a>".format(tag['href'], tag.get_text()))
else:
print(tag.get_text())
当解析器向下遍历树时,会返回多个片段/重复的内容:
HelloWorldFoo Bar BazExample Link: Google
HelloWorldFoo Bar BazExample Link: Google
HelloWorldFoo Bar BazExample Link: Google
HelloWorld
Hello
World
Foo Bar Baz
Baz
Example Link: Google
<a href='https://google.com'>Google</a>