简体   繁体   English

phpQuery-从没有类或ID的表中获取数据

[英]phpQuery - Get data from table with no class or id

I'm trying to get some data from a table, where the tag has no id, class or any other attribute that can identify it. 我正在尝试从表中获取一些数据,该表中的标签没有id,class或任何其他可以识别它的属性。

The table structure is something like this: 表结构是这样的:

<table>
<tr>
    <td><b>Type</b></td>
    <td>Plug and Play</td>
</tr>
<tr>
    <td><b>Model</b></td>
    <td>AH591Z</td>
</tr>
<tr>
    <td><b>Date</b></td>
    <td>02.04.2012</td>
</tr>
<tr>
    <td><b>Name</b></td>
    <td>CryptMachine</td>
</tr>
<tr>
    <td><b>ID</b></td>
    <td>9283</td>
</tr>
</table>

How would I for example get the Model, Name and ID but ignore the date and type, because I don't need those values. 例如,我将如何获取型号,名称和ID,但忽略日期和类型,因为我不需要这些值。

Is there a smart way to do this with phpQuery? 有没有聪明的办法做到这一点与phpQuery? So far I've just played around with the phpQuery library, but i definitely got the basics down, I just cannot wrap my brain around this. 到目前为止,我只是使用过phpQuery库,但是我确实掌握了基础知识,我只是无法解决这个问题。

I haven't tested this, but something like this maybe? 我还没有测试过,但是也许是这样?

$doc = phpQuery::newDocumentHTML($theDocument);
$table = $doc["table"];
$model = pq($table)->find("tr:eq(1) td:eq(1)")->text();
$id = pg($table)->find("tr:eq(4) td:eq(1)")->text();

If there is more than one table on the page, you may have to use eq(n) to specify which one, remember that they are enumerated from 0, not 1. 如果页面上有多个table ,则可能必须使用eq(n)指定哪个表,请记住,它们是从0而不是1枚举的。

UPDATE: If you don't know the order of the rows, you could use something like this (again, untested, sorry, but should get your pointed in the right direction) 更新:如果您不知道行的顺序,则可以使用类似的方法(再次,未经测试,对不起,但应指出正确的方向)

<?php

function getValue($table, $label) {
    $lable = strtolower($label);
    $rows = pq('tr', $table);

    foreach($rows as $row):
        if ( strtolower(pq($row)->find('td:eq(0)').text() ) === $label ):
            return pq($row)->find('td:eq(1)').text();
        endif;
    endforeach;
}

$doc = phpQuery::newDocumentHTML($theDocument);
$table = $doc["table"];

$model = getValue($table, "Model");
$id = getValue($table, "ID");

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM