如何在php DOMXPath对象中返回?

时间:2017-08-06 14:17:56

标签: php html laravel parsing domxpath

现在找到了查询' $ NotXP->查询' =查询返回字符串?!

如何使下一代码工作?

$xp = new \DOMXPath(@\DOMDocument::loadHTMLFile($url));

        $list = $xp->query('//table[@class="table-list quality series"] tbody');
        $link = $list->query('//tr[@class="item"]');

        $arr_links = [];

        foreach ($link as $link_in_cycle) {
            $link_quality = $link_in_cycle->query('//td[@class="column first video"]');
            $link_audio = $link_in_cycle->query('//td[@class="column audio"]');
            $link_size = $link_in_cycle->query('//td[@class="column size"]');
            $link_seed = $link_in_cycle->query('//td[@class="column seed-leech"] span[@class="seed"]');
            $link_download_url = $link_in_cycle->query('//td[@class="column last download"] a')->getAttribute("data-default");

html源请求@nigel-ren

从此代码中需要获取信息

   <tbody>
                                      <tr class="item">
                <td class="column first video">720x400</td>
                <td class="column audio">mp3</td>
                <td class="column size">5.70 Gb</td>
                <td class="column seed-leech">
                    <span class="seed">15</span>
                    <span class="leech">26</span>
                </td>
                <td class="column updated">07.07.2017</td>
                <td class="column consistence"><a href="javascript:void(0);" title="title in td" data-type="torrent-consistence" class="show-modal show-consistence" data-route="/hashinfo/12345?fields=files"></a></td>
                <td class="column last download">
                <a class="button middle rounded download zona-link"
   data-type="download"
   data-zona="0"
   data-torrent=""
   data-default="url_data"
   data-not-installed=""
   data-installed=""
   data-metriks="{'eventType': 'click', 'data' : { 'type': 'show_download', 'id': '84358'}}"
   title="text in title" href="javascript:void(0);" >Download</a>                </td>

1 个答案:

答案 0 :(得分:2)

我做了一些更改来帮助我调试代码。主要的是你的XPath表达式是无效的,你总是可以尝试像FreeFormatter这样的网站,它允许你用一些示例源来检查你的表达式。

$doc = new \DOMDocument();
$doc->loadHTMLFile($url);
$xp = new \DOMXPath($doc);

$list = $xp->query('//table[@class="table-list quality series"]//tr[@class="item"]');
$arr_links = [];

foreach ($list as $link_in_cycle) {

    $link_quality = $xp->query('//td[@class="column first video"]/text()', $link_in_cycle)[0]->wholeText;
    $link_audio = $xp->query('//td[@class="column audio"]/text()', $link_in_cycle)[0]->wholeText;
    $link_size = $xp->query('//td[@class="column size"]/text()', $link_in_cycle)[0]->wholeText;
    $link_seed = $xp->query('//td[@class="column seed-leech"]//span[@class="seed"]/text()', $link_in_cycle)[0]->wholeText;
    $link_download_url = $xp->query('//td[@class="column last download"]//a/@data-default', $link_in_cycle)[0]->value;

    echo $link_quality.PHP_EOL;
    echo $link_audio.PHP_EOL;
    echo $link_size.PHP_EOL;
    echo $link_seed.PHP_EOL;
    echo $link_download_url.PHP_EOL;
}

XPath表达式尝试并检索每个元素中的文本节点,这将返回所有节点的列表,此代码确实假设实际内容周围没有任何空格(并使用[0]来获取列表的第一个元素)。 wholetext只是DOMText元素的实际内容。

使用您提供的示例内容(加上我必须发明的周围位),它给出了......

720x400
mp3
5.70 Gb
15
Download