【问题标题】:Trying to implement a PHP crawler?尝试实现 PHP 爬虫?
【发布时间】:2013-08-19 05:07:31
【问题描述】:

我正在尝试在我的网站中实现 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;      
?>

【问题讨论】:

  • eBay 的 API 已经位于 here
  • 你读过documentation吗?看起来您正在 echo 处理一个对象($html 变量),但您不会这样做。

标签: php dom web-crawler


【解决方案1】:

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

$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);

【讨论】:

  • 谢谢它帮了我很多
猜你喜欢
  • 1970-01-01
  • 2021-05-08
  • 2010-09-06
  • 2013-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多