开发者

Scrape A Price Div Class From the Page Php

<?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-&开发者_StackOverflowgt;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>

The above just is outputting: <<<<<<<<<< anyone know why this is happening?


You have a syntax error:

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

Try this instead:

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

Also (I might be wrong), doesn't Google have an API for such requests?


Try this code instead:

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

And remove the echo $item[1]; line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜