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,应用于文件夹中的每个文本文件并生成新的文本文件


- 失败的 Canvas 360 jquery 插件 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Fetch API 如何获取响应体? 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Flexslider 箭头未正确显示 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01