- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有很多html文件,我想在每个html页面中提取表格和表格之外的其他信息,并将所有提取的信息合并到一个csv文件或制表符分隔文件中。虽然有"Best method of extracting text from multiple html files into one CSV file"的帖子,我用我的html数据试了一下,速度很快但是结果只有一列数据,当然它忽略了表外的信息。我已经将html文件预处理为output.txt
,其中包括我使用 bash 命令所需的表格内部或外部的信息:
#!/bin/bash
for f in '*.html'; do
cat $f | sed -n '/tbody><tr/,/\/tbody>/p' > output.txt
done;
它做得很好,它给我们留下了非常清晰的表格信息和我需要的其他信息。
output.txt的部分是这样的:
<tbody><tr><td><a href="fjzt-x.html?uid=NNNN">data11</a></td>
<td class="bzt">data12</td>
<td>data13</td>
<td>data14</td>
<td>data15</td>
<td>data16</td>
<td>data17</td>
<td class="tdb"><span id="sNNNNN"></span></td>
<td class="tdb"><span id="zfNNNNN"></span></td>
<td class="bzt">--</td><td></td>
</tr>
<script src="https://hq.sohujs.cn/list=data18" type="text/javascript" charset="gbk"></script>
<script type="text/javascript">getprice1('NNNNN',NNNN,NNN);</script>
</code></pre>
<td><a href="fjzt-x.html?uid=NNNN">data21</a></td>
<td class="bzt">data22</td>
<td>data23</td>
<td>data24</td>
<td>data25</td>
<td>data26</td>
<td>data27</td>
<td class="tdb"><span id="sNNNNN"></span></td>
<td class="tdb"><span id="zfNNNNN"></span></td>
<td class="bzt">--</td><td></td>
</tr>
<script src="https://hq.sohujs.cn/list=data28" type="text/javascript" charset="gbk"></script>
<script type="text/javascript">getprice1('NNNNN',NNNN,NNN);</script>
...
我想要像这样的制表符分隔的 Out Sample:
data11 data12 data13 data14 data15 data16 data17 data18
data21 data22 data23 data24 data25 data26 data27 data28
谁能帮帮我? Bash 或 python 命令会更好。
最佳答案
html 可能非常困惑。因此,我建议使用比 bash 脚本更高级的东西。由于您已经使用 python-tag 标记了问题(在以后的编辑中正确地替换为 bash 标记),让我们使用 python with BeautifulSoup。 .
编辑:在对此答案的评论中,OP 的作者阐明了 OP 真正想要的是什么:
如:
<td class="bzt">data12</td></code>
如:
<script src="hq.sohujs.cn/list=data18" type="text/javascript" charset="gbk"></script>
对当前工作目录中的所有 html 文件执行 1. 和 2.。
将其另存为一个 csv 表,其中的字段由 TAB 分隔 ("\t"
)。
python3 和 BeautifulSoup 的工作解决方案
我扩展了此答案早期版本的脚本来执行此操作,并在评论中添加了一些解释:
"""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>
关于linux - 如何将多个 html 文件中的表格提取到一个 csv 文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53616231/
我是一名优秀的程序员,十分优秀!