使用Python库'python-docx'来解析Word文档,并将其转换成HTML格式。然后,使用BeautifulSoup来解析新生成的HTML文件。以下是示例代码:
from docx import Document
from bs4 import BeautifulSoup
# 读取Word文档
doc = Document('example.docx')
# 将Word文件转换成HTML格式
html = ''
for paragraph in doc.paragraphs:
html += paragraph._element.xml
soup = BeautifulSoup(html, 'html.parser')
# 对新生成的HTML文件进行解析
...
注意:这种方法可能无法完整地转换Word文档中的所有内容,但通常可以解决大多数HTML解析问题。