尝试实现PHP爬虫程序?

尝试实现PHP爬虫程序?,php,dom,web-crawler,Php,Dom,Web Crawler,我正在尝试在我的网站上实现PHP爬虫。我的主要动机是从其他网站获取产品的价格。为此,我尝试使用dom解析器,但我的脚本不起作用。我解析类为prc的div的代码是:- <?php include('simplehtmldom/simple_html_dom.php'); $html = file_get_html('http://www.ebay.in'); $html->find('div', 1)->class = 'prc'; echo $html;

我正在尝试在我的网站上实现PHP爬虫。我的主要动机是从其他网站获取产品的价格。为此,我尝试使用dom解析器,但我的脚本不起作用。我解析类为prc的div的代码是:-

<?php
include('simplehtmldom/simple_html_dom.php');
$html = file_get_html('http://www.ebay.in');
$html->find('div', 1)->class = 'prc';   
        echo $html;      
?>

也许这有帮助(顺便说一句,它不需要SimpleHTMLDom):

$className='prc';//班级名称
$domDocument=新的domDocument('1.0');
@$domDocument->loadHTMLFile('http://www.ebay.in');
$domXPath=新的domXPath($domDocument);
//获取具有指定类名的所有元素
$prcs=$domXPath->query(
//*[包含(concat(“”,规范化空间(@class),“”,“$className”)]
);
对于($i=0;$i<$prcs->length;$i++){
//对于找到的每个项目,将其存储在$result中
$result[]=$prcs->item($i)->firstChild->nodeValue;
}
//显示结果
打印(结果);

eBay已经找到了他们的API。您阅读了吗?看起来您正在
echo
ing一个对象(
$html
变量),这不是您应该做的。Thanx它对我帮助很大
$className = 'prc'; // Name of the class

$domDocument = new DOMDocument('1.0');
@$domDocument->loadHTMLFile('http://www.ebay.in');
$domXPath = new DOMXPath($domDocument);

// Obtain all elements with the specified class name
$prcs = $domXPath->query(
    "//*[contains(concat(' ', normalize-space(@class), ' '), ' $className ')]"
);

for ($i = 0; $i < $prcs->length; $i++) {
    // For each item found, store it in $result
    $result[] = $prcs->item($i)->firstChild->nodeValue;
}

// Display results
print_r($result);