为了账号安全,请及时绑定邮箱和手机立即绑定

PHP:使用 xpath 从 html 表中提取多个数据

PHP:使用 xpath 从 html 表中提取多个数据

PHP
FFIVE 2021-11-05 16:21:41
我必须从 HTML 页面读取信息并将其传输到多个数组以进行进一步处理。我使用 xpath 的方法并没有那么成功,以至于我可以访问我想要的数据。正文部分包含一个具有不同行数的表格,如下例所示:...</tr><tr>    <td class="name" title="43PUS6551" datalabel="43PUS6551">        <span>43PUS6551</span>    </td>    <td datalabel="Internetnutzung" class="usage">eingeschränkt</td>    <td datalabel="Onlinezeit heute" class="bar time">        <span title="03:20 von 14:00 Stunden">            <span style="width:23.81%;"/>        </span>    </td>    <td datalabel="Zugangsprofil" class="profile">        <select name="profile:user6418">            <option value="filtprof1">Standard</option>            <option value="filtprof3">Unbeschränkt</option>            <option value="filtprof4">Gesperrt</option>            <option value="filtprof5334">Network</option>            <option value="filtprof5333" selected="selected">Stream</option>            <option value="filtprof4526">X-Box_One</option>        </select>    </td>    <td datalabel="" class="btncolumn">        <button type="submit" name="edit" id="uiEdit:user6418" value="filtprof5333" class="icon edit" title="Bearbeiten"/>    </td></tr><tr>...我需要一个数组,其中包含第title2 行中的属性作为键,并name从<select>部分(第 12 行)中获取该属性作为值。$devices = [    '43PUS6551' => 'profile:user6418'    …]我从这个开始,我能够收到这个数组的键:    $dom = new DOMDocument();    $dom->preserveWhiteSpace = false;    $dom->loadHTML($response);    $xmlSite = simplexml_import_dom($dom);    $devices = [];    $rows = $xmlSite->xpath('//tr/td[@title=@datalabel]');    foreach ($rows as $row) {        $key = utf8_decode((string)$row->attributes()['title']);但现在我正在努力获得指定的价值。我尝试了不同的方法:向上parent和向下回到节点<select>或使用following-sibling. 但是我太愚蠢了,无法正确使用 xpath 合成器。如果我做到了这一点,我需要一个数组,其中包含作为键name的<select>部分(第 12 行)的属性和作为值value的<option>部分的属性selcted。$filters = [    'profile:user6418' => 'filtprof5333'    …]最后,我需要一个包含该<option>部分数据的数组(出现在每一行中):$profiles = [    'Standard' => 'filtprof1',    'Unbeschränkt' => 'filtprof3,    …    'X-Box-One' => 'filtprof4526',]任何对正确的 xpath-hints 的帮助将不胜感激
查看完整描述

2 回答

?
开满天机

TA贡献1786条经验 获得超13个赞

尝试一下:


preg_match_all('/\<option value\="([a-z0-9]+)">([A-Za-z0-9\_\-]+)\<\/option\>/', $str, $match, PREG_SET_ORDER);

$profiles = array();

foreach($match as $row) {

  $profiles[$row[2]] = $row['1'];

}

print_r($profiles);


查看完整回答
反对 回复 2021-11-05
?
慕丝7291255

TA贡献1859条经验 获得超6个赞

我需要以下功能:


    // convert html response into SimpleXML

    $dom = new DOMDocument();

    $dom->preserveWhiteSpace = false;

    $dom->loadHTML($response);

    $xmlSite = simplexml_import_dom($dom);


    // initialize processing values

    $devices = [];

    $options = [];

    $filters = [];


    // parse SimpleXML with xpath to get current data

    $rows = $xmlSite->xpath('//tr/td[@title=@datalabel]');  // these are the rows with assignments of devices to filters

    foreach ($rows as $row) {

        $key = utf8_decode((string)$row->attributes()['title']);    // name (label) of the devices

        if (preg_match('/Alle /', $key)) {                          // skip standard settings

            continue;

        }

        $select = $row->xpath('parent::*//select[@name]');  // find the line with the currently assigned ID for the device

        $value = (string)$select[0]->attributes()['name'];  // get the current ID ('profile:user*' or 'profile:landevice*')

        $devices[$key] = $value;


        $options = $select[0]->xpath('option');             // the defined filters (dropdown in each row)

        foreach ($options as $option) {

            $profiles[utf8_decode((string)$option)] = (string)$option->attributes()['value'];   // get label and ID of filters

            if (isset($option->attributes()['selected'])) {     // determine the filter currently assigned to the device

                $filters[$value] = (string)$option->attributes()['value'];  // get device (ID) and filter (ID)

            }

        }

    }


查看完整回答
反对 回复 2021-11-05
  • 2 回答
  • 0 关注
  • 444 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信