【发布时间】:2018-11-18 11:29:39
【问题描述】:
我使用 os.walk 在文件夹中递归查找 html 文件。
这些 html 包含字符串。当 os.walk 建立一个列表时,我会用 BeautifulSoup
提取这些字符串
我尝试了以下代码,但它不起作用:
import os
from bs4 import BeautifulSoup
for root, dirs, files in os.walk ("mydir"):
for file in files:
if file.endswith (".html"):
print(os.path.join(root, file))
soup = BeautifulSoup(os.path.join(root, file), "html.parser")
soup.find all('a')
如何使用文件列表作为 BeautifulSoup 的输入? (并在 txt 文件中打印输出)
【问题讨论】:
-
在您第二次致电
os.path.join时,您错过了root。 -
我编辑了它,但没有任何改变
标签: python list beautifulsoup extract