Php 通过简单的HTMLDOM将html表中的数据提取到csv
我想将数据从html表提取到csv。我加载html数据并通过简单的HTMLDOM进行处理。这是我的样本数据Php 通过简单的HTMLDOM将html表中的数据提取到csv,php,html,csv,simple-html-dom,Php,Html,Csv,Simple Html Dom,我想将数据从html表提取到csv。我加载html数据并通过简单的HTMLDOM进行处理。这是我的样本数据 <tr id="ST1" class="foo"><td class="CLASS_0_NO_NEED"><span class="icons">pet</span></td><td class="name"><span class="name_icons">tom</span></td&
<tr id="ST1" class="foo"><td class="CLASS_0_NO_NEED"><span class="icons">pet</span></td><td class="name"><span class="name_icons">tom</span></td><td class="p_type"><span class="type_icons">cat</span></td><td class="p_sex"><span class="s_icons">male</span></td><td class="p_year"><span class="y_icons">2</span></td>
<td class="p_height"><span class="Height" alt="2|3">3</span></td>
<td class="p_weight"><span class="Weight" alt="3|5">5</span></td>
<td class="CLASS_7_NO_NEED">Close</td></tr>
<tr id="ST2" class="foo"><td class="CLASS_0_NO_NEED"><span class="icons">pet</span></td><td class="name"><span class="name_icons">sam</span></td><td class="p_type"><span class="type_icons">dog</span></td><td class="p_sex"><span class="s_icons">male</span></td><td class="p_year"><span class="y_icons">1</span></td>
<td class="p_height"><span class="Height" alt="4|4.5">4.5</span></td>
<td class="p_weight"><span class="Weight" alt="5|7">7</span></td>
<td class="CLASS_7_NO_NEED">Close</td></tr>
sample.csv的输出不是我真正想要的,因为每个字段中都有$DATA+空间
"pet ","tom ","cat ","male ","2 ","3 ","5 ",Close
"pet ","sam ","dog ","male ","1 ","4.5 ","7 ",Close
此外,我需要一些altHeight.alt和Weight.alt值到csv,我不想要一些类图标。丝状CV应该是这样的
"tom","cat","male","2","2","3","3","5"
"sam","dog","male","1","4","4.5","5","7"
可能吗?请帮助。先生,您不想使用jquery和javascript bcoz,因为这很简单 但是你可以试试php函数fputcsv 示例1 fputcsv示例
<?php
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
The above example will write the following to file.csv:
aaa,bbb,ccc,dddd
123,456,789
"""aaa""","""bbb"""
也许我没有给你讲清楚。我的程序是1。加载html文件petdata.html>2。简单dom进程>3。输出cvsyeah收到了,先生正在寻找。问题是不同的网站提供不同的方式。您有什么问题?如果是如何删除尾随空格,那么答案就是修剪
<?php
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
The above example will write the following to file.csv:
aaa,bbb,ccc,dddd
123,456,789
"""aaa""","""bbb"""