【问题标题】:Pulling 3rd party table data into array/json将第 3 方表数据拉入数组/json
【发布时间】:2017-09-25 23:03:53
【问题描述】:

我正在尝试使用 PHP 将此外部表数据转换为数组/JSON。我能够使用 XPath 并计算 td 等来做到这一点,但是,数据每周都会发生一些变化并且把所有事情都搞砸了......有没有一种好的方法来提取这些信息并使用条件语句来根据玩家的显示适当的值姓名?这是表格的链接See Here

我想得到类似的东西

Player name:
    GAMES:   
    MPR:
    PPR:
Player name:
    GAMES:   
    MPR:
    PPR:
etc...

如果有人能帮助我或指出正确的方向,我将不胜感激!这让我发疯了,如果有必要我什至会付钱。

谢谢!

这是我当前的代码

$urll = 'http://www.leagueleader.net/sharedreport.php?operatorid=98&code=1928e435-8dbe-450f-8bca-74f603f892f0';

$options = array (
    CURLOPT_RETURNTRANSFER => true,     // return web page
    CURLOPT_HEADER         => false,    // don't return headers
    CURLOPT_FOLLOWLOCATION => true,     // follow redirects
    CURLOPT_ENCODING       => "",       // handle all encodings
    CURLOPT_USERAGENT      => "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0", // something like Firefox 
    CURLOPT_AUTOREFERER    => true,     // set referer on redirect
    CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
    CURLOPT_TIMEOUT        => 120,      // timeout on response
    CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
);

$curl = curl_init($urll);
curl_setopt_array( $curl, $options );
$content = curl_exec($curl);
curl_close($curl);
$dom = new DOMDocument();
@$dom->loadHTML($content);
$xpath = new DOMXPath($dom); 

$tabless = $dom->getElementsByTagName('tbody'); 
$rows = $tabless->item(0)->getElementsByTagName('tr');

foreach ($rows as $roww) 
{ 

$colss = $roww->getElementsByTagName('td');

//$player = $cols->item(0)->nodeValue; $pstats[$i]['player'] = trim($player);
//$percentage = $cols->item(1)->nodeValue; $pstats[$i]['gamesplayed'] = trim($percentage);
$cricket = $colss->item(2)->nodeValue; $pstats[$j]['cricket'] = trim($cricket);
$o1 = $colss->item(3)->nodeValue; $pstats[$j]['01'] = trim($o1);


$j++;
} 

【问题讨论】:

  • 请显示您当前的代码,也许有人可以建议如何更改它。而不是为你编写代码。
  • 对“使用 PHP 抓取 HTML”(here's a tutorial for example) 进行一些研究,看看您自己编写代码的好处。 Stack Overflow 用于调试,而不是为您编写所有代码。
  • 如果他们改变了他们的结构,如果他们没有一些你可以访问的标准化 JSON 或 XML,你就必须每次都做出改变。如果您认为这将是他们一直生成的输出,并且没有其他可靠的方式来获取他们的数据,请考虑 PHP 的 DOMDocument

标签: php arrays json curl xpath


【解决方案1】:

您并不是在说 DOM 中发生了什么变化,因此很难制定“始终有效”的解决方案。

这是一个分两个阶段解析结果的解决方案。第一阶段从表中获取数据,然后第二阶段至少需要 4 个元素,否则继续。如果再次更改,应该很容易调试。

<?php
$doc = new DOMDocument();
$doc->loadHTML(file_get_contents('...'));
$doc->strictErrorChecking = false;

$pre = [];
foreach ($doc->getElementsByTagName('table') as $table) {
    foreach ($table->getElementsByTagName('tr') as $i => $tr) {
        $y = 0;
        foreach ($tr->childNodes as $td) {
            $text = trim($td->nodeValue);

            if ($y > 7) {
                unset($pre[$i]);
                continue;
            }

            if (empty($text)) {
                continue;
            }

            $pre[$i][] = $text;
            $y++;
        }
    }
}

// normalise
$result = [];
foreach ($pre as $row) {
    if (count($row) != 4 || $row[0] == 'Team Totals:') {
        continue;
    }

    if (!is_numeric($row[1]) || !is_numeric($row[2]) || !is_numeric($row[3])) {
        // looks broke again, send email to developer ;p
        continue;
    }

    $result[$row[0]] = [
        'name' => $row[0],
        'games' => $row[1],
        'mpr' => $row[2],
        'ppd' => $row[3]
    ];
}

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

/*
Array
(
    ['Lawrence Cherone'] => Array
        (
            [name] => Lawrence Cherone
            [games] => 51
            [mpr] => 5.00
            [ppd] => 67.48
        )

    ['Scott Sandberg'] => Array
        (
            [name] => Scott Sandberg
            [games] => 51
            [mpr] => 4.02
            [ppd] => 33.18
        )

*/
?>

根据结果构建表格:

<table>
    <thead>
        <tr>
            <?php foreach (array_values($result)[0] as $key => $row): ?>
            <th><?= ucfirst($key) ?></th>
            <?php endforeach ?>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($result as $key => $row): ?>
        <tr>
            <?php foreach ($row as $row): ?>
            <td><?= $row ?></td>
            <?php endforeach ?>
        </tr>
        <?php endforeach ?>
    </tbody>
</table>

或访问个别球员的统计数据:

<?= $result['Scott Sandberg']['games'] ?>

希望对你有帮助。

【讨论】:

  • 你是个了不起的人,非常感谢你...Tbh我不确定发生了什么变化,我在过去几天使用节点计数让它工作,今天当它更新时它抛出到处都是数字。通过查看更新的表格,除了日期之外,我真的没有看到任何变化......我认为这种对 4 个元素的检查会很好地工作。如果您不介意,还有一件事,我正在尝试单独回应内容...这对我来说有点新,所以这是我正在尝试的方式,这似乎不起作用..
  • if ($result-&gt;name == 'Josh Slom') { echo $row[2]; }
  • 你想把它放在桌子上吗?
  • 我对每个玩家都有一个 div,我想把他们的 MPR、PPD 等放在他们的名字下。
  • 酷,更新,见底部。注意$result[$row[0]] PHP 位的变化。
猜你喜欢
  • 2012-04-28
  • 2018-06-06
  • 1970-01-01
  • 1970-01-01
  • 2012-02-18
  • 1970-01-01
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多