【发布时间】:2016-09-10 00:10:22
【问题描述】:
我一直在尝试从 HTML 表中提取数据的不同方法,例如使用 xpath。这些表不包含任何类,所以我不确定如何在没有类或 Id 的情况下使用 xpath。正在从 rss xml 文件中检索此数据。我目前正在使用 DOM。提取数据后,我将尝试按职位对表格进行排序
这是我的 php 代码
$html='';
$xml= simplexml_load_file($url) or die("ERROR: Cannot connect to url\n check if report still exist in the Gradleaders system");
/*What we do here in this loop is retrieve all content inside the encoded content,
*which includes the CDATA information. This is where the HTML and styling is included.
*/
foreach($xml->channel->item as $cont){
$html=''.$cont->children('content',true)->encoded.'<br>'; //actual tag name is encoded
}
$htmlParser= new DOMDocument(); //to parse html using DOMDocument
libxml_use_internal_errors(true); // your HTML gives parser warnings, keep them internal
$htmlParser->loadHTML($html); //Loaded the html string we took from simple xml
$htmlParser->preserveWhiteSpace = false;
$tables= $htmlParser->getElementsByTagName('table');
$rows= $tables->item(0)->getElementsByTagName('tr');
foreach($rows as $row){
$cols = $row->getElementsByTagName('td');
echo $cols;
}
这是我从中提取信息的 HTML
<table cellpadding='1' cellspacing='2'>
<tr>
<td><b>Job Title:</b></td>
<td>Job Example </td>
</tr>
<tr>
<td><b>Job ID:</b></td>
<td>23992</td>
</tr>
<tr>
<td><b>Job Description:</b></td>
<td>Just a job example </td>
</tr>
<tr>
<td><b>Job Category:</b></td>
<td>Work-study Position</td>
</tr>
<tr>
<td><b>Position Type:</b></td>
<td>Work-study</td>
</tr>
<tr>
<td><b>Applicant Type:</b></td>
<td>Work-study</td>
</tr>
<tr>
<td><b>Status:</b></td>
<td>Active</td>
</tr>
<tr>
<td colspan='2'><b><a href='https://www.myjobs.com/tuemp/job_view.aspx?token=I1iBwstbTs2pau+SjrYfWA%3d%3d'>Click to View More</a></b></td>
</tr>
</table>
【问题讨论】:
-
你需要提取什么?
-
好吧,我需要解析表内的所有数据。我有很多这样的表格,因为这是一个 rss 提要。整个目标是能够根据职称按照字母顺序重新组织所有表格
-
您需要
table中的文本或html 吗?请使用所需输出的示例更新您的问题。 -
我需要 Html,我只需要能够抓取标签 td 来查看它是什么职位,这样我就可以进行相应的排序。我会更新