Web Scraper-PHP-简单的html dom

时间:2018-08-13 19:13:03

标签: php web-scraping

我正在抓取电子商店。我有网址列表,加载它们,然后刮擦桌子。好。第一页是正确的,它可能有30个参数。并且下一页如果有20个参数,并且在复制它们之前没有该页面中的参数。那就是我的问题,为什么?也许我只是不知道如何清除阵列,您能帮我吗?

//The code
foreach($odkazy as $odkaz) {
	
	//Here i am loading each page
	$html = new simple_html_dom();
	$html->load_file($odkaz);
  	
	//Finding code of prodcut
	$ptyp = $html->find("span.p-type");
	foreach($ptyp as $a)
	$item["p-type"] = trim($a->plaintext);



	//Here are values of product
	$hodnoticky = $html->find("dd");
	foreach($hodnoticky as $h) 
	$hodnota[] = preg_replace('/\s\s+/', ' ',$h->plaintext);
	
	//Here are parameters
	$parametrici = $html->find("dt");
	foreach($parametrici as $p) 
	$parametr[] = preg_replace('/\s\s+/', ' ',$p->plaintext);

	//Here i am mixing them.
	foreach($parametr as $i => $key) 
	$item[trim($key)] = $hodnota[$i];
	
					
	$items[] = $item;


	
	$html->clear();
	unset($html);

}

print_r($items);

2 个答案:

答案 0 :(得分:0)

似乎在循环之间永远不会取消设置项目数组。试试这个:

get

答案 1 :(得分:0)

哦。谢谢@JacobW。问题出在另一个数组中..我试图取消设置$ item ..但解决方案是取消设置带有值和参数的数组。谢谢!

	$items[] = $item;
	$html->clear();
	unset($parametr);
	unset($hodnota);
	unset($item); 
	unset($html);