我是從一個網站的源代碼是麻煩與PHP
view-source:http://www.pakdukaan.com/75-computer-cases
我使用抽取數據的代碼報廢的數據是這樣的
<?php
$html = file_get_contents('http://www.pakdukaan.com/75-computer-cases');
$pk_doc = new DOMDocument();
libxml_use_internal_errors(TRUE);
if(!empty($html)){
$pk_doc->loadHTML($html);
libxml_clear_errors();
$pk_xpath = new DOMXPath($pk_doc);
$pk_list = array();
$pk_and_price = $pk_xpath->query('//div[@class="product_list list row "]');
if($pk_and_price->length > 0){
foreach($pk_and_price as $pat){
$name = $pk_xpath->query('//h5[@class="name"]', $pat)->item(0)->nodeValue;
$pkmn_types = array();
$price = $pk_xpath->query('//span[@class="price product-price"]', $pat)
foreach($types as $type){
$pkmn_types[] = $type->nodeValue;
}
$pk_list[] = array('name' => $name, 'price' => $pkmn_price);
}
}
}
//output what we have
echo "<pre>";
echo print_r($pk_list);
echo "</pre>";
?>
而是獲得所有病例名字,我得到一個和其他的東西是我得到的案件的所有價格的兩倍。
這是輸出
Array
(
[0] => Array
(
[name] =>
Thermaltake V2 Plus + 350W Power Supply
[price] => Array
(
[0] =>
Rs. 4,099
[1] =>
Rs. 4,099
[2] =>
Rs. 5,899
[3] =>
Rs. 5,899
[4] =>
Rs. 8,499
[5] =>
Rs. 8,499
[6] =>
Rs. 9,499
[7] =>
Rs. 9,499
[8] =>
Rs. 10,350
[9] =>
Rs. 10,350
[10] =>
Rs. 12,999
[11] =>
Rs. 12,999
[12] =>
Rs. 17,799
[13] =>
Rs. 17,799
[14] =>
Rs. 16,199
[15] =>
Rs. 16,199
[16] =>
Rs. 17,299
[17] =>
Rs. 17,299
[18] =>
Rs. 16,500
[19] =>
Rs. 16,500
[20] =>
Rs. 5,899
[21] =>
Rs. 5,899
[22] =>
Rs. 8,399
[23] =>
Rs. 8,399
[24] =>
Rs. 4,999
[25] =>
Rs. 4,999
[26] =>
Rs. 7,599
[27] =>
Rs. 7,599
[28] =>
Rs. 9,999
[29] =>
Rs. 9,999
)
)
)
1
誰能幫出了問題?我已經嘗試了很多改變網站的源代碼中的div的類,但無法得到適當的結果。
再次檢查您的html結構。 –