2011-09-18 83 views
2
<?php 

    # don't forget the library 
    include('simple_html_dom.php'); 

    # this is the global array we fill with article information 
    $Prices = array(); 

    getPrices('http://www.google.com/search?q=xbox+360&tbm=shop&hl=en&aq=f'); 

function getPrices($page) { 
    global $Prices, $descriptions; 

    $html = new simple_html_dom(); 
    $html->load_file($page); 

    $items = $html->find('div.psliprice'); 

    foreach($items as $post) { 
     # remember comments count as nodes 
     $Prices[] = $post->children(0)->outertext; 
    } 
} 

?> 


<html> 
<head> 
    <style> 
     #main { 
      margin: 80px auto; 
      width: 600px; 
     } 
     h1 { 
      font: bold20px/30px verdana, sans-serif; 
      text-decoration: none; 
     } 
     p { 
      font: 10px/14px verdana, sans-serif; 
    </style> 
</head> 
<body> 
    <div id="main"> 
<?php 
    foreach($Prices as $item) { 
     echo $item[0]; 
     #echo $item[1]; 
    } 
?> 
    </div> 
</body> 
</html> 

以上只是输出:<<<<<<<<<<任何人都知道这是为什么发生?从页面刮价格分类Php

回答

1

你有一个语法错误:

$items = $html->find('div[class=psliprice]"'); 

试试这个:

$items = $html->find('div[class="psliprice"]'); 

而且(我可能是错的),没有谷歌有这样的请求的API?


试试这个代码,而不是:

$Prices[] = $post->children(0)->outertext; 

,并删除了echo $item[1];线。

+0

查看更新的代码和相关的错误请 – m67

+0

查看我的更新。 DOM似乎更简单一些。 – Blender

+0

做这两个编辑不会做任何事情。请参阅更新的代码。 – m67