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

我必须从 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 的帮助将不胜感激


FFIVE
浏览 449回答 2
2回答

开满天机

尝试一下:preg_match_all('/\<option value\="([a-z0-9]+)">([A-Za-z0-9\_\-]+)\<\/option\>/', $str, $match, PREG_SET_ORDER);$profiles = array();foreach($match as $row) {&nbsp; $profiles[$row[2]] = $row['1'];}print_r($profiles);

慕丝7291255

我需要以下功能:&nbsp; &nbsp; // convert html response into SimpleXML&nbsp; &nbsp; $dom = new DOMDocument();&nbsp; &nbsp; $dom->preserveWhiteSpace = false;&nbsp; &nbsp; $dom->loadHTML($response);&nbsp; &nbsp; $xmlSite = simplexml_import_dom($dom);&nbsp; &nbsp; // initialize processing values&nbsp; &nbsp; $devices = [];&nbsp; &nbsp; $options = [];&nbsp; &nbsp; $filters = [];&nbsp; &nbsp; // parse SimpleXML with xpath to get current data&nbsp; &nbsp; $rows = $xmlSite->xpath('//tr/td[@title=@datalabel]');&nbsp; // these are the rows with assignments of devices to filters&nbsp; &nbsp; foreach ($rows as $row) {&nbsp; &nbsp; &nbsp; &nbsp; $key = utf8_decode((string)$row->attributes()['title']);&nbsp; &nbsp; // name (label) of the devices&nbsp; &nbsp; &nbsp; &nbsp; if (preg_match('/Alle /', $key)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // skip standard settings&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $select = $row->xpath('parent::*//select[@name]');&nbsp; // find the line with the currently assigned ID for the device&nbsp; &nbsp; &nbsp; &nbsp; $value = (string)$select[0]->attributes()['name'];&nbsp; // get the current ID ('profile:user*' or 'profile:landevice*')&nbsp; &nbsp; &nbsp; &nbsp; $devices[$key] = $value;&nbsp; &nbsp; &nbsp; &nbsp; $options = $select[0]->xpath('option');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// the defined filters (dropdown in each row)&nbsp; &nbsp; &nbsp; &nbsp; foreach ($options as $option) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $profiles[utf8_decode((string)$option)] = (string)$option->attributes()['value'];&nbsp; &nbsp;// get label and ID of filters&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isset($option->attributes()['selected'])) {&nbsp; &nbsp; &nbsp;// determine the filter currently assigned to the device&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $filters[$value] = (string)$option->attributes()['value'];&nbsp; // get device (ID) and filter (ID)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP