html 可能非常混乱。因此,我建议使用比 bash 脚本更高级的东西。由于您已经使用 python-tag 标记了问题(在以后的编辑中正确地替换为 bash 标记),让我们使用python with BeautifulSoup。
编辑:在对此答案的 cmets 中,OP 的作者澄清了 OP 真正想要的是什么:
- 在一个html表格中收集td标签的内容。
如:
<td class="bzt">data12</td></code>
- 另外从同一 html 文件中一个或多个脚本标记的 src 属性中的链接收集数据。
如:
<script src="hq.sohujs.cn/list=data18" type="text/javascript" charset="gbk"></script>
对当前工作目录中的所有 html 文件执行 1. 和 2.。
将此保存为 csv 表格,其中字段由 TAB ("\t") 分隔。
python3 和 BeautifulSoup 的工作解决方案
我从这个答案的早期版本扩展了脚本来做到这一点,并在 cmets 中添加了一些解释:
"""module import"""
from bs4 import BeautifulSoup
import glob
"""obtain list of all html files in cwd"""
filenames = glob.glob("*.html")
for filename in filenames:
"""parse each file with bs4"""
soup = BeautifulSoup(open(filename), 'html.parser')
"""obtain data from td tags"""
tdTextList = [td.text.strip().replace("\n","") for td in soup.find_all("td")]
"""clean data: remove empty strings"""
tdTextList = [td for td in tdTextList if not td=='']
"""obtain data from script tag attributes"""
scriptTags = soup.findAll("script")
for elementTag in scriptTags:
src_attribute = elementTag.attrs.get("src")
if src_attribute is not None:
src_elements = src_attribute.split("=")
if len(src_elements) > 1:
tdTextList.append(src_elements[1])
"""write data to output002.csv"""
with open("output002.csv", "a") as outputfile:
for tdText in tdTextList:
outputfile.write(tdText)
outputfile.write("\t")
outputfile.write("\n")
如何运行
从 html 文件所在目录中的终端执行:
python3 <script_name.py>
或者,您可以将工作目录移动到脚本开头的正确位置(html 文件所在的位置):
import os
os.chdir("</path/to/directory>")
python2 和 BeautifulSoup 的工作解决方案
由于OP的作者要求提供python2版本,我在这里提供一个。与上述 python3 版本的唯一区别是文件处理程序(python2 使用 file(),而不是 open())。
"""module import"""
from bs4 import BeautifulSoup
import glob
"""obtain list of all html files in cwd"""
filenames = glob.glob("*.html")
for filename in filenames:
"""parse each file with bs4"""
soup = BeautifulSoup(file(filename), 'html.parser')
"""obtain data from td tags"""
tdTextList = [td.text.strip().replace("\n","") for td in soup.find_all("td")]
"""clean data: remove empty strings"""
tdTextList = [td for td in tdTextList if not td=='']
"""obtain data from script tag attributes"""
scriptTags = soup.findAll("script")
for elementTag in scriptTags:
src_attribute = elementTag.attrs.get("src")
if src_attribute is not None:
src_elements = src_attribute.split("=")
if len(src_elements) > 1:
tdTextList.append(src_elements[1])
"""write data to output002.csv"""
with file("output002.csv", "a") as outputfile:
for tdText in tdTextList:
outputfile.write(tdText)
outputfile.write("\t")
outputfile.write("\n")
运行python2版本类似于上面的python3。
此答案的旧版本
以下脚本执行您所描述的操作:
收集当前目录下所有html文件的全部内容
将它们写入带有制表符分隔符的 csv。
这是一个示例脚本:
from bs4 import BeautifulSoup
import glob
filenames = glob.glob("*.html")
tdTextList = []
for filename in filenames:
soup = BeautifulSoup(open(filename), 'html.parser')
tdTextList += [td.text for td in soup.find_all("td")]
with open("output001.csv", "w") as outputfile:
for tdText in tdTextList:
outputfile.write(tdText)
outputfile.write("\t")
这就是你所描述的。这可能不是你想要的。
请注意,这将生成一个包含单个非常长行的文件(您无需指定何时需要新行)。如果任何 td 标记的内容包含换行符,它可能会意外生成格式错误的文件。
为了使输出文件看起来更好一些,让我们为每个读取的 html 文件写一个新行,并在将数据写入输出之前从数据中删除前导和尾随空格以及换行符。
from bs4 import BeautifulSoup
import glob
filenames = glob.glob("*.html")
for filename in filenames:
soup = BeautifulSoup(open(filename), 'html.parser')
tdTextList = [td.text.strip().replace("\n","") for td in soup.find_all("td")]
with open("output002.csv", "a") as outputfile:
for tdText in tdTextList:
outputfile.write(tdText)
outputfile.write("\t")
outputfile.write("\n")
注意:您可以使用以下命令从 bash shell 运行任一脚本:
python3 <script_name.py>