【问题标题】:How to extracting Data from HTML table using php如何使用 php 从 HTML 表中提取数据
【发布时间】: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 来查看它是什么职位,这样我就可以进行相应的排序。我会更新

标签: php html dom xpath


【解决方案1】:

您可以使用xpathquery('//td') 并使用C14N() 检索td html,类似于:

$dom = new DOMDocument();
$dom->loadHtml($html);
$x = new DOMXpath($dom);
foreach($x->query('//td') as $td){
    echo $td->C14N();
    //if just need the text use:
    //echo $td->textContent;
}

输出:

<td><b>Job Title:</b></td>
<td>Job Example </td>
<td><b>Job ID:</b></td>
...

C14N();

在失败时将规范化节点作为stringFALSE 返回


更新:

另一个问题,我如何获取单个表数据?例如, 抓紧,Job ID

使用XPathcontains,即:

foreach($x->query('//td[contains(., "Job ID:")]') as $td){
    echo $td->textContent;
}

更新 V2:

在那之后我怎样才能得到下一个表数据(实际得到工作 身份证)?

使用following-sibling::*[1],即:

echo $x->query('//td[contains(*, "Job ID:")]/following-sibling::*[1]')->item(0)->textContent;
//23992

【讨论】:

  • 对不起,请忽略我的最后一条消息。太感谢了。我一直在研究一个星期来解决这个问题。你能指导我一些很好的资源来进行这种类型的解析吗?另一个问题,如何获取单个表数据?比如随便抓,Job ID?
  • 很抱歉问了这么多问题,我只是觉得你是我从这里遇到的最好的资源。关于获取单个表数据(例如 Job Id)的最后一个问题,之后如何获取下一个表数据(以实际获取 Job Id)?前面说了,我有很多表,每个Job Id都是唯一的,那么如何从表中去下一个表数据
  • NP,检查新的更新。我现在去吃点东西;)GL
  • 这些是一些非常深奥和令人陶醉的 XPath,最重要的是,根本不是很便携。不幸的是,他必须用一种以上的语言来做这件事——或者如果开发人员更改了一个字段的名称,或者在字段名和冒号之间添加了一个空格,或者为了其他东西而避开冒号......
  • 您说的是哪种“另一种语言”
【解决方案2】:
$xpathParser = new DOMXPath($htmlParser);
$tableDataNodes = $xpathParser->evaluate("//table/tr/td")
for ($x=0;$x<$tableDataNodes.length;$x++) {
    echo $tableDataNodes[$x];
}

【讨论】:

  • 谢谢,我会尽快尝试您的解决方案,Keith
猜你喜欢
  • 1970-01-01
  • 2011-04-08
  • 2020-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
  • 2011-10-16
相关资源
最近更新 更多