【问题标题】:BeautifulSoup on multiple .html files多个 .html 文件上的 BeautifulSoup
【发布时间】:2014-12-24 08:52:54
【问题描述】:

我正在尝试通过使用此处建议的模型enter link description here 来使用 BeautifulSoup 提取固定标签之间的信息@

我的文件夹中有很多 .html 文件,我想将使用 BeautifulSoup 脚本获得的结果以单独的 .txt 文件的形式保存到另一个文件夹中。这些.txt 文件应与原始文件同名,但仅包含提取的内容。我编写的脚本(见下文)成功处理文件,但不会将提取的位写入单个文件。

import os
import glob
from bs4 import BeautifulSoup

dir_path = "C:My_folder\\tmp\\"

for file_name in glob.glob(os.path.join(dir_path, "*.html")):
    my_data = (file_name)
    soup = BeautifulSoup(open(my_data, "r").read())
    for i in soup.select('font[color="#FF0000"]'):
        print(i.text)
        file_path = os.path.join(dir_path, file_name)
        text = open(file_path, mode='r').read()
        results = i.text
        results_dir = "C:\\My_folder\\tmp\\working"
        results_file = file_name[:-4] + 'txt'
        file_path = os.path.join(results_dir, results_file)
        open(file_path, mode='w', encoding='UTF-8').write(results)

【问题讨论】:

    标签: python python-2.7 beautifulsoup text-files extract


    【解决方案1】:

    Glob 返回完整路径。您正在为找到的每个 font 元素重新打开文件,替换文件的内容。将文件的打开移到循环之外;你真的应该使用文件作为上下文管理器(使用with 语句)以确保它们也再次正确关闭:

    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(os.path.join(results_dir, results_file), 'w') as outfile:        
            for i in soup.select('font[color="#FF0000"]'):
                print(i.text)
                outfile.write(i.text + '\n')
    

    【讨论】:

    • @meshfields 感谢您指出这一点;我一定是忘记加入基本文件名了。
    【解决方案2】:
    import glob
    import os
    from BeautifulSoup import BeautifulSoup
    
    input_dir = "/home/infogrid/Desktop/Work/stack_over/input/"
    #- Already Present on system.
    output_dir = "/home/infogrid/Desktop/Work/stack_over/output/"
    
    for file_name in glob.glob(input_dir+ "*.html"):
        with open(file_name) as fp:
            soup = BeautifulSoup(fp)
            results_file = "%s%s.txt"%(output_dir, os.path.splitext(os.path.basename(file_name))[0])
            tmp = [i.text for i in soup.findAll('font') if i.get("color")=="#FF0000"]
            with open(results_file, 'w') as fp:        
                print "\n".join(tmp)
                fp.write("\n".join(tmp))
    

    【讨论】:

    • 这也有效。我喜欢有不止一种方法可以执行相同的任务。谢谢你的建议。很遗憾,Stackflow 上没有选择接受这两种解决方案。
    猜你喜欢
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    • 2020-07-27
    • 1970-01-01
    • 2015-10-30
    • 2017-10-28
    • 1970-01-01
    相关资源
    最近更新 更多