使用Simple HTML Dom刮擦表格

时间:2014-04-24 20:43:59

标签: php simple-html-dom

我正试图抓住这个产品表,

http://www.dropforyou.com/search2.php?mode=search&posted_data%5Bcategoryid%5D=2&posted_data%5Bsearch_in_subcategories%5D=on

我需要产品ID,数量和价格。

由于网站使用cookies和帖子表单,我使用CURL抓取网站。哪个工作正常。然后我用$ html = str_get_html($ content)加载到简单的html dom中;

我已经能够将所有表值加载到数组中,但是我无法标记它们。他们只是以0,1,2进来,我无法说出是什么。

我尝试使用在stackoverflow上发布的不同方法,但是它给了我致命错误:在

中的非对象上调用成员函数find()

我的工作代码没有标记

$content = curlscraper($urltoscrape);

$html = str_get_html($content);

$tds = $html->find('table',2)->find('td');

$num = NULL;
foreach($tds as $td)
{
    $num[] = $td->plaintext;
}

echo '<pre>';
var_dump ($num);
echo '</pre>';

我在Stackoverflow上找到的代码只给出了致命错误:在

中的非对象上调用成员函数find()
$content = curlscraper($urltoscrape);

$html = str_get_html($content);

foreach($html->find('tr',2) as $page)
{
$item['sku'] = $page->find('td',0)->plaintext;
$item['product'] = $page->find('td',1)->plaintext;
$item['Qty'] = $page->find('td',2)->plaintext;
$item['description'] = $page->find('td',3)->plaintext;
$item['price'] = $page->find('td',4)->plaintext;

$table[] = $item;
}

print_r($table);

1 个答案:

答案 0 :(得分:0)

尝试为foreach函数初始化变量,然后使用您的代码。但你不能说出哪一行产生了这个错误?

 $line = $html->find('tr',2);

 foreach($line as $page)
{
//var_dump($page) //You can check array
$item['sku'] = $page->find('td',0)->plaintext;
$item['product'] = $page->find('td',1)->plaintext;
$item['Qty'] = $page->find('td',2)->plaintext;
$item['description'] = $page->find('td',3)->plaintext;
$item['price'] = $page->find('td',4)->plaintext;

$table[] = $item;
}