【发布时间】:2014-10-16 03:14:47
【问题描述】:
我有 2 个 php 文件(index.php 和 lelong.php)。我试图首先在 index.php (表)中加载 html 并在第二列上显示单词(Calculating...),而 lelong.php 在输出之前从网站中提取数据。
有没有办法做到这一点?我听说过使用 JS 或 AJAX,但不确定如何使用。
索引.php
<!DOCTYPE html>
<html>
<head>
<?php include 'lelong.php'; ?>
</head>
<body>
<table border ="1" style = "width:50%">
<tr>
<td>E-Commerce Website</td>
<td>No. of Products </td>
</tr>
<tr>
<td>Lelong</a></td>
<td><?php echo $lelong; ?></td>
</tr>
</table>
<body>
lelong.php
<?php
$grep = new DoMDocument();
@$grep->loadHTMLFile("http://www.lelong.com.my/Auc/List/BrowseAll.asp");
$finder = new DomXPath($grep);
$class = "CatLevel1";
$nodes = $finder->query("//*[contains(@class, '$class')]");
$total_L = 0;
foreach ($nodes as $node) {
$span = $node->childNodes;
$search = array(0,1,2,3,4,5,6,7,8,9);
$number = str_replace($search, '', $span->item(1)->nodeValue);
$number = preg_replace("/[^0-9]/", '', $span->item(1)->nodeValue);
$total_L += (int) $number;
}
$lelong = number_format( $total_L , 0 , '.' , ',' );
?>
谢谢
【问题讨论】:
-
如果您对 Ajax 有基本的了解,jQuery's documentation 非常可靠。 Ajax 将满足您的需求,但如果您尝试自己完成并向我们展示您尝试的解决方案,我们可以更好地帮助您。
标签: php html xpath web-scraping domdocument