【发布时间】:2019-08-30 15:41:36
【问题描述】:
我正在使用此代码: BeautifulSoup on multiple .html files 此代码将提取的文本保存到 .txt 文件中。我想将 DataFrame 中提取的每条记录保存为单独的行。
我想将结果作为“文件”作为单列保存到 DataFrame 中。如何达到同样的效果?
import glob
import os.path
from bs4 import BeautifulSoup
dir_path = r"C:\My_folder\tmp"
results_dir = r"C:\My_folder\tmp\working"
for file_name in glob.glob(os.path.join(dir_path, "*.html")):
with open(file_name) as html_file:
soup = BeautifulSoup(html_file)
results_file = os.path.splitext(file_name)[0] + '.txt'
with open(results_file, 'w') as outfile:
for i in soup.select('font[color="#FF0000"]'):
print(i.text)
outfile.write(i.text + '\n')
【问题讨论】:
-
您能否提供到目前为止您尝试用来解决此问题的代码?我们需要看看你试图帮助你什么。 :)
-
我已经附上了代码。
标签: python