Python - beautifulsoup, apply in every text file in folder and produce new text file(Python - beautifulsoup,应用于文件夹中的每个文本文件并生成新的文本文件)
问题描述
我正在使用以下 Python - Beautifulsoup 代码从文本文件中删除 html 元素:
I am using the following Python - Beautifulsoup code to remove html elements from a text file:
from bs4 import BeautifulSoup
with open("textFileWithHtml.txt") as markup:
soup = BeautifulSoup(markup.read())
with open("strip_textFileWithHtml.txt", "w") as f:
f.write(soup.get_text().encode('utf-8'))
我的问题是如何将此代码应用于文件夹(目录)中的每个文本文件,并为每个文本文件生成一个新的文本文件,该文件已被处理并删除了 html 元素等,而无需为每个文本文件调用函数?
The question I have is how can I apply this code to every text file in a folder(directory), and for each text file produce a new text file which is processed and where the html elements etc. are removed, without having to call the function for each and every text file?
推荐答案
我会将这项工作留给操作系统,只需将硬编码的输入文件替换为来自外部源的输入,在 argv
数组中,然后在循环内或使用匹配许多文件的正则表达式调用脚本,例如:
I would leave that work to the OS, simply replace the hardcoded input file with input from external source, in argv
array, and invoke the script inside a loop or with a regular expression that matches many files, like:
from bs4 import BeautifulSoup
import sys
for fi in sys.argv[1:]:
with open(fi) as markup:
soup = BeautifulSoup(markup.read())
with open("strip_" + fi, "w") as f:
f.write(soup.get_text().encode('utf-8'))
然后像这样运行它:
python script.py *.txt
这篇关于Python - beautifulsoup,应用于文件夹中的每个文本文件并生成新的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python - beautifulsoup,应用于文件夹中的每个文本文件并生成新的文本文件


基础教程推荐
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01