【发布时间】:2022-01-10 14:36:35
【问题描述】:
我正在尝试在 python 中以 utf-8 格式打开文件。 我在 htmls 路径中有列表,我创建列表的代码工作:
def get_all_htmls(directory_path):
return glob.iglob(os.path.join(directory_path,'*.html'))
directory_path=r'C:\Users\astar\Project\Articles\Articles'
links = []
for html_path in get_all_htmls(directory_path):
links.append(html_path)
但是,现在在这段代码中:
for link in links:
f=codecs.open(r'link','r','utf-8')
document= BeautifulSoup(f)
所有的 html 都不工作,我能做什么?
【问题讨论】:
-
Python 3 字符串是 Unicode,
open的默认值已经是 UTF-8。您无需执行任何操作即可读取 UTF8 文件。如果您有问题,则表示文件不是 UTF8
标签: python python-3.x encoding