在PHP中使用curl和xpath解析HTML页面 [英] Parsing an HTML page using curl and xpath in PHP

查看:151
本文介绍了在PHP中使用curl和xpath解析HTML页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析此网页 https://www.galliera.it/118 彩色条下的数字。

I need to parse this web page https://www.galliera.it/118 getting the numbers under the coloured bars.

这是我的代码(不起作用!)...

This is my code (that doesn't work!!) ...

<?php
    ini_set('display_errors', 1);

    $url = 'https://www.galliera.it/118';

    print "The url ... ".$url;
    echo '<br>';
    echo '<br>';

    //#Set CURL parameters ...
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_PROXY, '');
    $data = curl_exec($ch);
    curl_close($ch);

    //print "Data ... ".$data;
    //echo '<br>';
    //echo '<br>';

    $dom = new DOMDocument();
    @$dom->loadHTML($data);

    $xpath = new DOMXPath($dom);

    // This is the xpath for a number under a bar ....
    // /html/body/div[2]/div[1]/div/div/ul/li[6]/span
    // How may I get it?
    // The following code doesn't work, it's only to show my goals ..

    $greenWaitingNumber = $xpath->query('/html/body/div[2]/div[1]/div/div/ul/li[6]/span');
    $theText = (string).$greenWaitingNumber;

    print "Data ... ".$theText;
    echo '<br>';
    echo '<br>';

?>

有任何建议/示例/替代方法吗?

Any suggestions / examples / alternatives?

推荐答案

这是您的php脚本,用于按排序好的数组挖掘数据,您可以查看脚本的结果并根据需要更改结构。

Here is your php script that is mining request by you data in nicely sorted array, you can see the results of script and change the structure as you need it. Cheers!

$html = file_get_contents("https://www.galliera.it/118");

$dom = new DOMDocument();
$dom->loadHTML($html);
$finder = new DOMXPath($dom);

// find all divs class row
$rows = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' row ')]");

$data = array();
foreach ($rows as $row) {
    $groupName = $row->getElementsByTagName('h2')->item(0)->textContent;
    $data[$groupName] = array();

    // find all div class box
    $boxes = $finder->query("./*[contains(concat(' ', normalize-space(@class), ' '), ' box ')]", $row);
    foreach ($boxes as $box) {
        $subgroupName = $box->getElementsByTagName('h3')->item(0)->textContent;
        $data[$groupName][$subgroupName] = array();

        $listItems = $box->getElementsByTagName('li');
        foreach ($listItems as $k => $li) {

            $class = $li->getAttribute('class');
            $text = $li->textContent;

            if (!strlen(trim($text))) {
                // this should be the graph bar so kip it
                continue;
            }

            // I see only integer numbers so I cast to int, otherwise you can change the type or event not cast it
            $data[$groupName][$subgroupName][] = array('type' => $class, 'value' => (int) $text);
        }
    }
}

echo '<pre>' . print_r($data, true) . '</pre>';

,输出类似于:

Array
(
    [SAN MARTINO - 15:30] => Array
        (
            [ATTESA: 22] => Array
                (
                    [0] => Array
                        (
                            [type] => rosso
                            [value] => 1
                        )

                    [1] => Array
                        (
                            [type] => giallo
                            [value] => 12
                        )

                    [2] => Array
                        (
                            [type] => verde
                            [value] => 7
                        )

                    [3] => Array
                        (
                            [type] => bianco
                            [value] => 2
                        )

                )

            [VISITA: 45] => Array
                (
                    [0] => Array
                        (
                            [type] => rosso
                            [value] => 5
                        )
...

这篇关于在PHP中使用curl和xpath解析HTML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆