【发布时间】:2020-11-21 15:22:07
【问题描述】:
我发现this 关于解析 TEI XML 的文章在处理单个 XML 文件时非常有用。但是,我有一个完整的目录。我通过目录的循环没有执行,我不知道为什么。
from bs4 import BeautifulSoup
import os.path
import glob
tei_docs = "../input/tei-xml-files"
def read_tei(tei_docs):
os.chdir(tei_docs)
for i in glob.glob(os.path.join(tei_docs, "*.xml")):
read_file(i)
def read_file(i):
with open(i, "r") as tei:
soup = BeautifulSoup(tei, 'lxml')
soup.title.getText()
我得到了输出
名称错误
Traceback(最近一次通话最后一次)
在
----> 1个soup.title.getText()
NameError: name 'soup' 没有定义
【问题讨论】:
标签: python html beautifulsoup html-parsing