我尝试用简单的dom解析一个网站,但我只有一个结果。
下面你有两个功能,一个只显示价格和工作正常。
第二个必须显示一些元素,如标题,价格,特殊但我只有一个结果。
我的代码中有什么问题?
谢谢
$url = 'https://www.cdiscount.com/search/10/samsung+galaxy+s7.html#_his_';
$argument = 'div.prdtPrSt';
function extractAllPrice($url, $argument) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 10.10; labnol;) ctrlq.org");
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($curl);
curl_close($curl);
if (is_null($html) || $html === false) {
$html = file_get_contents($url);
if (is_null($html)) {
print_r('error no content');
return false;
}
}
$content = str_get_html($html);
foreach($content->find($argument) as $price) {
preg_match_all('!\d+!', $price, $matches);
$price_extracted = (float)implode('.', $matches[0]);
$result[] = $price_extracted;
}
return $result;
}
var_dump(extractAllPrice($url, $argument));
结果
array (size=15)
0 => float 527.4
1 => float 437.94
2 => float 637.95
3 => float 487.54
4 => float 589
5 => float 527.4
6 => float 437.94
7 => float 487.54
8 => float 637.95
9 => float 555
10 => float 500
11 => float 714.25
12 => float 350.9
13 => float 770.26
14 => float 589
第二个功能
$url = 'https://www.cdiscount.com/search/10/samsung+galaxy+s7.html#_his_';
$argument_content = 'div.lpTopTDG';
$argument_title = 'div.prdtBTit';
$argument_special_price = 'span.price';
$argument_price = 'div.prdtPrSt';
function extractAllPriceTest($url, $argument_content = null, $argument_title= null, $argument_special_price = null, $argument_price = null) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 10.10; labnol;) ctrlq.org");
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($curl);
curl_close($curl);
$error = false;
if (is_null($html) || $html === false) {
$html = file_get_contents($url);
$error = false;
if (is_null($html)) {
print_r('error no content');
$error = true;
return false;
}
}
$content = str_get_html($html);
foreach($content->find($argument_content) as $article) {
$item['title'] = $article->find($argument_title, 0)->plaintext;
$item['special_price'] = $article->find($argument_special_price, 0)->plaintext;
$item['normal_price'] = $article->find($argument_price, 0)->plaintext;
preg_match_all('!\d+!', $item['special_price'], $matches);
$item['special_price'] = (float)implode('.', $matches[0]);
preg_match_all('!\d+!', $item['normal_price'], $matches);
$item['normal_price'] = (float)implode('.', $matches[0]);
$result[] = $item;
}
return $result;
}
var_dump(extractAllPriceTest($url, $argument_content, $argument_title, $argument_special_price, $argument_price));
结果
array (size=1)
0 =>
array (size=3)
'title' => string 'Samsung Galaxy S7 Noir' (length=22)
'special_price' => float 419
'normal_price' => float 527.4