【发布时间】:2014-06-17 23:13:39
【问题描述】:
我正在尝试从 play 和 amazon 获取个人项目的价格,但我有 2 个问题。 首先,我有游戏可以工作,但它得到了错误的价格,其次,亚马逊没有得到任何结果。
这是我一直在尝试的代码。
$playdotcom = file_get_contents('http://www.play.com/Search.html?searchstring=".$getdata[getlist_item]."&searchsource=0&searchtype=r2alldvd');
$amazoncouk = file_get_contents('http://www.amazon.co.uk/gp/search?search-alias=dvd&keywords=".$getdata[getlist_item]."');
preg_match('#<span class="price">(.*)</span>#', $playdotcom, $pmatch);
$newpricep = $pmatch[1];
preg_match('#used</a> from <strong>(.*)</strong>#', $playdotcom, $pmatch);
$usedpricep = $pmatch[1];
preg_match('#<span class="bld lrg red"> (.*)</span>#', $amazoncouk, $amatch);
$newpricea = $amatch[1];
preg_match('#<span class="price bld">(.*)</span> used#', $amazoncouk, $amatch);
$usedpricea = $amatch[1];
然后回显结果:
echo "Play :: New: $newpricep - Used: $usedpricep";
echo "Amazon :: New: $newpricea - Used: $usedpricea";
让你知道发生了什么
$getdata[getlist_item] = "American Pie 5: The Naked Mile";
工作正常。
知道为什么这些不能正常工作吗?
编辑:我刚刚意识到 file_get_contents 中的$getdata[getlist_item] 没有使用该变量,只是按原样打印该变量...为什么这样做???
【问题讨论】:
-
旁注 Dont use regex for HTML parsing,使用像DOMDocument这样的适当解析器或像simplehtmldom这样的工具
标签: php regex file-get-contents